简体   繁体   中英

Different reponses for mongodb query using callback and promise

Have you any idea why I don't get any error in the first example? But if I use a callback, I get MongoTimeoutError.

await server.stop();
try {
    const r = await db.things.insertOne({ a: 1 }); // no error, r is undefined
    assert(!r);
} catch (err) {
    console.log(err);
}
await server.restart();
const r = await db.things.insertOne({ a: 1 });
assert(r.insertedCount === 1);
db.things.insertOne({ a: 1 }, (err, result) => {
    if (err) {
        //"MongoTimeoutError" "Server selection timed out after 10000 ms"
        return reject(err);
    }
    resolve(result);
})

In options I have bufferMaxEntries = 0 .

Probably the first code snippet is also suffering from the same timeout error that's why it does not return the inserted object. If you don't see any console logs and get any exception about that, it is about insertOne method's implementation.

In the second way by providing an arrow function which takes an error parameter, you happen to be able to identify the error.

There is an issue with mongodb driver. See the JIRA ticket

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM