Promisify 一个函数,创建 mongoose 模型实例(即文档),将其保存到模型中,然后返回然后返回模型

发布于 2025-01-16 18:40:51 字数 605 浏览 3 评论 0原文

我正在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2025-01-23 18:40:51

创建 mongoose 模型实例(即文档)、将其保存到模型并返回然后返回模型的函数

您无需执行任何特殊操作。 .save() 已经返回(承诺)已保存的文档。返回它。

const Definition = mongoose.model('Definition', mySchema);

const saveDefinition = (data) => {
  const newDef = new Definition(data);
  return newDef.save();
};

完毕。


我会以不同的方式编写它以摆脱该全局 Definition 变量:

const saveObject = modelName => {
  const Model = mongoose.model(modelName, mySchema);
  return (data) => new Model(data).save();
};

const saveDefinition = saveObject('Definition');
const saveWhatever = saveObject('Whatever');

在两种情况下用法是相同的

saveDefinition({ /* ... */ }).then(def => {
  // success
}).catch(err => {
  // failure
});

async () => {
  try {
    const def = await saveDefinition({ /* ... */ });
    // success
  } catch (err) {
    // failure
  }
};

a function that creates a mongoose model instance (i.e. a document), saves it to the model, and returns then returns the model

There is nothing special you need to do. .save() already returns (a promise for) the saved document. Return it.

const Definition = mongoose.model('Definition', mySchema);

const saveDefinition = (data) => {
  const newDef = new Definition(data);
  return newDef.save();
};

Done.


I would write it differently to get rid of that global Definition variable:

const saveObject = modelName => {
  const Model = mongoose.model(modelName, mySchema);
  return (data) => new Model(data).save();
};

const saveDefinition = saveObject('Definition');
const saveWhatever = saveObject('Whatever');

Usage is the same in both cases

saveDefinition({ /* ... */ }).then(def => {
  // success
}).catch(err => {
  // failure
});

or

async () => {
  try {
    const def = await saveDefinition({ /* ... */ });
    // success
  } catch (err) {
    // failure
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文