【发布时间】:2019-02-20 18:48:35
【问题描述】:
我有一个 GraphQL 突变,它尝试使用 Mongoose 将对象保存到 MongoDB 集合:
Mutation: {
addPost: (parent, args) => {
let output = {};
const newpost = new dbPost({
_id: new mongoose.Types.ObjectId(),
title: args.title,
content: args.content,
author: {
id: args.authorid,
first_name: args.authorfirstname,
last_name: args.authorlastname,
}
});
newpost.save().then((result) => {
output = result;
});
return output // returns null, need result!
},
}
脚本工作得很好,因为它成功地将对象(通过 args 传递给它)保存到集合中。但是,我无法返回从 .then() 内部返回的对象以进行进一步处理。在 GraphiQL 中,响应是一个空对象。有什么办法,我可以在 .then() 之外返回 result 的值?
【问题讨论】:
标签: mongodb mongoose promise graphql