Я получаю эту ошибку, и запрос должен быть выполнен только один раз,
UnhandledPromiseRejectionWarning: ParallelSaveError: Can't save() the same doc multiple times in parallel. Document: 5e269bd87c588b6285499c14
at ParallelSaveError.MongooseError [as constructor] (/root/react-passport-example/node_modules/mongoose/lib/error/mongooseError.js:10:11)
at new ParallelSaveError (/root/react-passport-example/node_modules/mongoose/lib/error/parallelSave.js:18:17)
at model.Model.save (/root/react-passport-example/node_modules/mongoose/lib/model.js:434:20)
at /root/react-passport-example/server/routes/public_api.js:64:21
at /root/react-passport-example/node_modules/mongoose/lib/model.js:4590:16
at /root/react-passport-example/node_modules/mongoose/lib/query.js:4351:12
at cb (/root/react-passport-example/node_modules/mongoose/lib/query.js:1900:14)
at result (/root/react-passport-example/node_modules/mongodb/lib/operations/execute_operation.js:75:17) at executeCallback (/root/react-passport-example/node_modules/mongodb/lib/operations/execute_operation.js:68:9)
at handleCallback (/root/react-passport-example/node_modules/mongodb/lib/utils.js:129:55)
at cursor.close (/root/react-passport-example/node_modules/mongodb/lib/operations/to_array.js:36:13) at handleCallback (/root/react-passport-example/node_modules/mongodb/lib/utils.js:129:55)
at completeClose (/root/react-passport-example/node_modules/mongodb/lib/cursor.js:859:16)
at Cursor.close (/root/react-passport-example/node_modules/mongodb/lib/cursor.js:878:12)
at cursor._next (/root/react-passport-example/node_modules/mongodb/lib/operations/to_array.js:35:25) at self._initializeCursor (/root/react-passport-example/node_modules/mongodb/lib/core/cursor.js:750:9) (node:25221) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:25221) [DEP0018] 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.
Мне интересно, есть ли лучший и более чистый способ сделать следующее.
router.post('/login', (req, res) => {
var query = req.body,
add = new LoginModel(query);
console.log(query.email);
LoginModel.find({uid : req.body.uid}, function (err, docs) {
if (docs.length){
console.log(err);
res.status(200).json({
message: "ERROR USER ALREADY EXISIT",
// user values passed through from auth middleware
//user: req.user
});
}else{
add.save(add.save(function (err, query) {
if (err) return console.error(err);
}));
}
});
});