【发布时间】:2021-04-13 21:58:11
【问题描述】:
我有一个 express api 端点。
我的 createS 服务是这样的:
async function createS(name, user_ids) {
const seg = new Seg ({
name : name,
user_ids : user_ids
})
await seg.save().then(doc => {
console.log(doc);===============>>> not undefined
return {doc}
}).catch(err => {console.log(err)});
}
我的控制器是这样的:
async function createS(req,res,next){
const sName = req.body.name;
const user_ids = req.body.user_ids;
console.log("*****")
segService.createS(sName, user_ids).then(function (doc){
console.log(doc)=============================================>>> undefined
})
我知道这个问题取决于异步过程。我尝试了几种类型的解决方案,我也是 Node JS 的新手。
为什么会出现这个错误?我该如何解决这个错误?
【问题讨论】:
-
您没有从
createS函数返回任何内容。此外,将async/await与then混合是一个坏主意。
标签: javascript node.js express asynchronous async-await