Promisify 一个函数,创建 mongoose 模型实例(即文档),将其保存到模型中,然后返回然后返回模型
我正在尝试 Promisify 以下函数
let Definition = mongoose.model('Definition', mySchema)
let saveDefinition = (newDefinition) => {
var newDef = new Definition(newDefinition);
newDef.save();
return Definition.find();
}
来实现以下事件序列
let saveDefinition = (newDefinition) => {
return = new Promise((res, rej) => {
// newDef = new Definition(newDefinition)
// then
// newDef.save()
// then
// return Definition.find()
})
}
目标是根据客户端的请求调用此函数,将文档保存到名为“Definition”的模型中,然后返回模型中的所有文档给客户。任何帮助或指导将不胜感激。
我不太确定如何解决这个问题
I'm trying to Promisify the following function
let Definition = mongoose.model('Definition', mySchema)
let saveDefinition = (newDefinition) => {
var newDef = new Definition(newDefinition);
newDef.save();
return Definition.find();
}
to achieve the following sequence of events
let saveDefinition = (newDefinition) => {
return = new Promise((res, rej) => {
// newDef = new Definition(newDefinition)
// then
// newDef.save()
// then
// return Definition.find()
})
}
The goal is to invoke this function upon a request from the client, save a document to the model called "Definition" and return all of the documents within the model back to to the client. Any help or guidance would be greatly appreciated.
I'm not really sure on how to approach the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需执行任何特殊操作。
.save()
已经返回(承诺)已保存的文档。返回它。完毕。
我会以不同的方式编写它以摆脱该全局
Definition
变量:在两种情况下用法是相同的
或
There is nothing special you need to do.
.save()
already returns (a promise for) the saved document. Return it.Done.
I would write it differently to get rid of that global
Definition
variable:Usage is the same in both cases
or