I'm using Node 7.2.1 with the new async/await feature. I'm also using the Native ES6 Promises with mongoose like this -
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
My code flow 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 = []; //some syntactical error in mongo query to produce exception
const followers = await User.aggregate(aggregateQuery);
return followers.map(follower => follower.followerData);
}
catch (err) {
return Promise.reject(err);
}
};
This code works absolutely fine. Problem arises when there's some error produced. So I purposely modified my mongoose query so that MongoDB would throw an error.
Now MongoDB, as expected throws an error which is perfectly caught by my code and returned to the client with a 400 error code.
The Problem is that even though the error(intentional) has been 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 it can be seen my request has returned the 400 status and my error log has also been printed from the initial method's catch block but Node.js is still saying that the error message is not handled.
Why is it saying this even after the same error has been caught?
Update - Thanks to @dvlsg and @Bergi, the bug has been fixed in version 4.7.5