@Amir-61 ^^
When i use to upsertWithWhere() 2 times in one endpoint i have following error occurs:
{ Error: There are multiple instances found.Upsert Operation will not be perform
ed!
at C:\Users\ketan\Desktop\akash\node_modules\loopback-datasource-juggler\l
ib\dao.js:847:25
at C:\Users\ketan\Desktop\akash\node_modules\async-listener\glue.js:188:31
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback [as _tickCallback] (internal/process/next_tic
k.js:128:9) statusCode: 400 }
Exactly same issue here try to iterate an array using upserWithWhere I recive an error message with: There are multiple instances found.Upsert Operation will not be perform
ed!
@shrimaliakash This is for me an issue on logic, try to do a reduce instead of a map, I think it is due to nature of find or create..
I don't think so. The logic is fine because it works with a simple operations like create and update using a conditional, but my intention was do not use a conditional if exist an upsertWithWhere method.
@diegoazh actually with create or simple updates this is ok, but not for a findOrCreate or a upsertWithWhere.
Let me explain why...
The flow for a findOrCreate or a upsertWithWhere goes like this:
Promise start (call this fase1)... find instances that match where clause ( this is a promise, call this fase2 ) -> if instance found... then create/update instance ( this is a promise too, call this fase3 )
so, lets say you have this flow in a map where we use 3 findOrCreates.. this means we would have 3 flows for findOrCreate...
The execution of this promises due to the nature of javascript/nodejs goes like this:
flow1 fase1 -> flow2 fase1-> flow3 fase1 -> flow1 fase2 -> flow2 fase2 -> flow3 fase2 -> flow 1 fase 3 -> flow1 fase3 ( here find or create would create multiple instances ).....
So is a logic problem findOrCreates, or upsertWithWhere can't be done in parallel. and the find part is executed in parallel too, as result, none of the promise would find the object, and gonna create it more than once. The same happen with the upsertWithWhere..
Hope this help. This only happen when you are iterating array of async operations that contains more async operations inside, and due to the nature of javascript/nodejs, some async actions performs at the same time, in this case the find.
regards.
Most helpful comment
@diegoazh actually with create or simple updates this is ok, but not for a
findOrCreateor aupsertWithWhere.Let me explain why...
The flow for a
findOrCreateor aupsertWithWheregoes like this:Promise start (call this fase1)... find instances that match where clause ( this is a promise, call this fase2 ) -> if instance found... then create/update instance ( this is a promise too, call this fase3 )
so, lets say you have this flow in a map where we use 3 findOrCreates.. this means we would have 3 flows for findOrCreate...
The execution of this promises due to the nature of javascript/nodejs goes like this:
flow1 fase1 -> flow2 fase1-> flow3 fase1 -> flow1 fase2 -> flow2 fase2 -> flow3 fase2 -> flow 1 fase 3 -> flow1 fase3 ( here find or create would create multiple instances ).....
So is a logic problem findOrCreates, or upsertWithWhere can't be done in parallel. and the
findpart is executed in parallel too, as result, none of the promise would find the object, and gonna create it more than once. The same happen with the upsertWithWhere..Hope this help. This only happen when you are iterating array of async operations that contains more async operations inside, and due to the nature of javascript/nodejs, some async actions performs at the same time, in this case the
find.regards.