NodeJS MongoDB populate() bringing string instead of JSON - node.js

I have a product and I have a property that brings an array with a provider id and its price. The idea is that every product has an array with multiples providers, each one with its own price.
However, when I use populate() looking for ProviderID, it brings me populated as string instead of a JSON. Below is GraphQL query return from a query that bring products.
ProviderPrice class, inside ProductModel
Optional parameter in ProductModel
This is how I am executing populate
Has anyone gone through this?

Found the problem.
I was using the class ProviderPrice instead of the model ProviderPriceModel.

Related

TypeORM querybuilder with take,skip and order by

I have an issue with a NestJs service that uses Typeorm and implements pagination logic.
When I use limit and offset on a query I execute using QueryBuilder, it does not return all results (i.e. limit 10 returns only 8 results)- it does work however, meaning that the relations aren't mapped yet still filtered on, orderby does order by a relation field without mapping it too.
The issue began when I changed my limit and offset to take and skip.
It gets buggy when I use order by on a relation column I innerJoin.
For example:
querybuilder = querybuilder.innerJoin(profile.service, service)
then try to do
queryBuilder = queryBuilder.orderBy("service.price", "ASC")
It returns an error
column distinctAlias.service_price does not exist
That's because it looks like you must leftJoinAndSelect the relation in order to have the field alias included in the query.
I don't want to select the relations but only the base entity. Is there any way around it?
Limit and offset as pagination utilities are not an option since in the docs itself it says to not use them when appending joins to the query.

Changing collection name according to query parameters in Nestjs

Can we get data from different collections of same or multiple mongo databases according to the query parameters in nest js ?
For example if parameter says get data from collection A, then collection A data should be displayed if it says get data from collection B, then collection B data should be displayed.
Can we do it in same controller or we need to make multiple controllers ?
I got it, I just have to make two models from different collections and use the desired model according to query parameter by using simple if then else.

GraphQL: react-apollo client overwrites array elements

I have a GraphQL query that returns an array of items. I can run the query in GraphiQL and get the result I expected.
I have a react application that uses react-apollo and the #graphql decorator to resolve the very same query to component props. When running the query via react-apollo, every item in the returned array is the same - each item has been 'overwritten' by the last item.
I can see in the dev-tools network tab that the correct array items were sent over the network, so the issue seems to relate to react-apollo. What could be causing react-apollo to overwrite array elements?
The issue was that my qgl fragment for the query did not include the id property for the items in the Array.
If you have a similar issue, ensure your schema includes an id or _id property on each item; that each id value is unique, and that you are requesting the id property in your query. Alternatively you can supply a dataIdFromObject function in your client to dynamically generate IDs for values.
You can read more in the Apollo docs for Normalization with dataIdFromObject

Trying to do a where filter in findOne and in Node api returns empty array when filtering by ids in loopback

Trying to do a where filter in findOne and in Node api returns empty array when filtering by ids in loopback
https://url/api/Model1/findOne?filter={"where":{"attrs":"id"}}
where id is exactly 24 digits long
It is possible that you simple don't have an object with that ID in that particular collection in the database. In such a case you should get an empty array.
Or you may need to find by id and not by attr - it depends on how the relevant field in your database is named.
See the docs, there are good examples there: https://loopback.io/doc/en/lb2/Where-filter.html
E.g. this:
http://localhost:3000/api/Books?filter={"where":{"or":[{"id":1},{"id":2}]}}
Note that the id is used and not attr. See what is your field in your case.

Mongoose renaming _id to id

I'm creating an API that is using data from Mongoose. The front end (Backbone) expects id, instead of _id. I cannot seem to find an easy solution to such a simple problem. Is anyone aware of a way to rename _id to id. I want this to be default behavior across every schema.
Did you think of setting model.idAttribute to _id on the front end (Backbone). This would allow Backbone to 'transparently map that key to id'.
http://backbonejs.org/#Model-idAttribute
You can set up a schema method getItem which returns the desired fields and id = _id, if you really need that :)

Resources