I am using Node 7.2.1 with the new async / await function. I also use Native ES6 Promises with a mongoose-like -
const mongoose = require('mongoose'); mongoose.Promise = global.Promise;
My code stream is like this:
async function getFollowers(){ try { const followers = await User.getFollowersFromMongo(req.params.userId); res.send(followers); } catch (err) { winston.error('Printing Error = ', err); res.status(400).send({success: false, error: err}); } } UserSchema.statics.getFollowersFromMongo = async(userId) => { try { let aggregateQuery = [];
This code is working absolutely fine. A problem occurs when some error occurs. Therefore, I intentionally changed my request to mongoose so that MongoDB throws an error.
Now MongoDB, as expected, generates an error that gets into my code perfectly and returns to the client with error code 400.
The problem is that even if the error (intentional) was caught by me, Node.js still gives me this warning -
error: Printing Error = MongoError: path option to $unwind stage should be prefixed with a '$': followerData at Function.MongoError.create (/home/node_modules/mongodb-core/lib/error.js:31:11) at /home/node_modules/mongodb-core/lib/connection/pool.js:483:72 at authenticateStragglers (/home/node_modules/mongodb-core/lib/connection/pool.js:429:16) at Connection.messageHandler (/home/node_modules/mongodb-core/lib/connection/pool.js:463:5) at Socket.<anonymous> (/home/node_modules/mongodb-core/lib/connection/connection.js:317:22) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at Socket.Readable.push (_stream_readable.js:134:10) at TCP.onread (net.js:551:20) GET /user/385/followers 400 39.868 ms - 263 (node:10158) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: path option to $unwind stage should be prefixed with a '$': followerData (node:10158) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
As you can see, my request returned status 400, and my error log was also printed from the catch block of the source code, but Node.js still says that the error message is not being processed.
Why does he talk about this even after the error has been caught?
Update - Thanks to @dvlsg and @Bergi, the bug was fixed in version 4.7.5