Hi,
I’m prototyping an node.js app, using the free tier, using Mongoose library (I don’t think this is the cause of the issue though).
I followed instructions here:
to create an auto increment orderId field in my orders collection documents upon create.
When I save() the object, I get the object back from the call, the orderId is not present.
If I go check the the object in Atlas, it is there. I decided then to retrieve the object by Id again, to see if it would appear. It would appear some times.
I then retrieved the object a 2nd time, and it would appear more often.
I then retrieved the object a 3rd time, and now I see the orderId consistently.
I’m not familiar with the workings of mongodb and how they do these triggers, so I assume there is some sort of timing issue.
Upon save I’m returned the object before the trigger has executed. I tried to do a {j; true} option to see if it would return properly, but was not successful.
I saw that if the trigger is called a lot, it might be put on an execution queue, but I’m just testing out the api, so that should not be the issue.
Could it be because I’m on the free tier?
Am I missing something here?
Thanks for your help,
Rich
newOrder = await newOrder.save();
//console.log(newOrder);
console.log("ID: " + newOrder.id);
console.log("orderId 1: " + newOrder.orderId);
let updatedOrder = await Order.findById(newOrder.id).populate({
path: 'orderItems',
populate: { path: 'menuItem', select: "name" }//description
});
console.log("orderId 2: " + updatedOrder.orderId);
updatedOrder = await Order.findById(newOrder.id).populate({
path: 'orderItems',
populate: { path: 'menuItem', select: "name" }//description
});
updatedOrder = await Order.findById(newOrder.id).populate({
path: 'orderItems',
populate: { path: 'menuItem', select: "name" }//description
});
console.log("orderId 3: " + updatedOrder.orderId);
Output
ID: 61f8358055301a3970af47f4
orderId 1: undefined
orderId 2: undefined
orderId 3: 68