本文介绍了猫鼬不会在回调中返回保存的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用猫鼬保存新记录.我没有在回调中获取保存的文档.
I am trying to save a new record using mongoose. I am not getting the saved document in the callback.
app.post("/register",(req,res) => {
let userData = req.body;
let user = new User(userData)
user.save().then((err,doc) => {
res.json({"success":true,"data":doc});
console.log(doc);
})
});
我正在获取文档:1.虽然我应该得到整个文件.请帮帮我.
I am getting doc:1. While I should get the whole document. Please help me.
"dependencies": {
"body-parser": "^1.18.2",
"crypto-js": "^3.1.9-1",
"express": "^4.15.5",
"mongoose": "^4.11.13"
}
推荐答案
您使用的是 promise,then
回调仅提供一个参数 - 异步调用的结果.要捕获错误,应使用 catch
回调:
You're using promises, then
callback provides only one parameter - result of asynchronous call. To catch error, catch
callback should be used:
app.post("/register", (req, res) => {
let userData = req.body;
let user = new User(userData);
user
.save()
.then(doc => {
console.log(doc);
res.json({ success: true, data: doc });
})
.catch(err => {
console.log(err);
res.status(500).send({ error: err });
});
});
这篇关于猫鼬不会在回调中返回保存的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!