Closed govindrai closed 6 years ago
@govindrai when you return Promise.all(proms
, are you awaiting the response? e.g. await myFunctionThatReturnsPromiseDotAll();
?
If so, can you create a complete example like the one below that demonstrates your expectations and shows the failure?
#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const DB = '7171';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };
const schema = new Schema({
name: String
});
const Test = mongoose.model('test', schema);
const tests = '0123456789'.split('').map(d => ({ name: `test${d}`}));
function myFunctionThatReturnsPromiseDotAll(docs) {
const proms = docs.map(d => {
return Test.updateOne({ _id: d._id }, { name: `${d.name}_take2` });
});
return Promise.all(proms);
}
async function test() {
const updated = await Test.find({});
assert.strictEqual(updated.filter(u => /_take2/.test(u.name)).length, 10);
}
async function run() {
await mongoose.connect(URI, OPTS);
await connection.dropDatabase();
const docs = await Test.create(tests);
assert.ok(docs);
assert.strictEqual(docs.length, 10);
await myFunctionThatReturnsPromiseDotAll(docs);
await test();
console.log('All Assertions Pass.');
await connection.close();
}
run();
issues: ./7171.js
All Assertions Pass.
issues:
@lineus sincerely apologize for wasting your time.. The reason these calls were never making it was because I was passing an array of promises into an array and passing that final array into a Promise.all
call (i.e. Promise.all([[arrayOfPromises],[arrayOfPromises]...)
). And so Promise.all was resolving the arrays immediately.. 🤦♂️
However, thank you for providing that example file..I really like how you have formatted your test in a new database. I will use this as a template to test my assertions in the future before raising an issue.
Super appreciate your response, nonetheless.
@govindrai no apologies necessary. I'm glad you got it sorted out!
Using the latest Mongoose 5.3.5 and node 8.11.4.
Here's some code that I am using to send updateOne commands:
All other mongoose calls are working perfectly. this refers to a User document. Values are valid as can be seen from an example query.
Here's the query actually printed out. I have my debugging turned on and I can't find mongoose's debug statement printed for this operation, neither is anything changing in the nothing changes in the DB
Currently having to find the document and then update it using document.prototype.updateOne