【问题标题】:Node JS Mongoose Async CallbacksNode JS Mongoose 异步回调
【发布时间】:2018-10-27 19:01:07
【问题描述】:

我有这段代码,我似乎有点搞混了。 它的作用是创建用户。现在,如果用户拥有一家公司,那么该公司应该与用户一起创建并相应地链接。如果公司已经存在,则不应创建它,也不应将其归因于用户。

代码首先查找一家公司,如果找不到,则创建一家。生活很好。但是,如果我要在我的“if (!company)”检查中添加一个 else,我将复制我的大部分创建用户代码。我也相信我无法检查公司,然后像我通常用不同的语言那样同步运行用户创建。因此我有点卡住了..

module.exports = {
  postUsers: (req, res) => {
    'use strict'
    Company.findOne({name: req.body.company}, (err, company) => {
      if (err) {
        Logger.error(err)
        return res.send(500, err)
      }
      if (!company) {
        // only attribute a company if one doesn't exist
        // don't want users to assign themselves to existing companies automatically
        // need approval in place from an existing company member
        let newCompanyToAdd = new Company({
          name: req.body.company
        })
        newCompanyToAdd.save(err => {
          if (err) {
            Logger.error(err)
            return res.send(500, err)
          }
          let user = new User({
            username: req.body.username,
            password: req.body.password,
            firstname: req.body.firstname,
            lastname: req.body.lastname,
            company: newCompanyToAdd.id
          })
          user.save(err => {
            if (err) {
              return res.send(500, err)
            }
            res.status(200).json({ message: 'New User Added' })
          })
        })
      }
    })
  }

编辑#

  postUsers: (req, res) => {
    'use strict'
    let user = new User({
      username: req.body.username,
      password: req.body.password,
      firstname: req.body.firstname,
      lastname: req.body.lastname
    })
    Company.findOne({name: req.body.company}, (err, company) => {
      if (err) {
        Logger.error(err)
        return res.send(500, err)
      }
      if (!company && req.name.company !== undefined) {
        // only attribute a company if one doesn't exist
        // don't want users to assign themselves to existing companies automatically
        // need approval in place from an existing company member
        let newCompanyToAdd = new Company({
          name: req.body.company
        })
        newCompanyToAdd.save(err => {
          if (err) {
            Logger.error(err)
            return res.send(500, err)
          }
          user.company = newCompanyToAdd._id
        })
      }
    })
    user.save(err => {
      if (err) {
        return res.send(500, err)
      }
      res.status(200).json({ message: 'New User Added' })
    })
  }

【问题讨论】:

    标签: node.js mongoose asynccallback


    【解决方案1】:

    我不完全确定我了解总体目标。但似乎您担心添加用户代码被复制,因为无论公司是否已经存在,您都需要添加用户。有什么原因不能先保存用户,在回调中,必要时有条件地创建公司?

    【讨论】:

    • 我可以,但如果需要,我必须用 companyId 更新用户,这将是一个额外的 dB 调用?
    • 如果你不太担心先出现的时间(创建公司和创建用户),那么在你找到现有公司之后,你可以在用户到找到的 id 或 null。然后,在 if 循环之外只保存用户。这里改变的一件事是保存公司和用户都是异步的。除此之外,我只是将保存用户逻辑分解为它自己的函数并简单地调用它。无论如何,您很可能需要在其他地方使用此功能。
    • 我还没有抽象出保存部分,但这是你的想法吗?见编辑
    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 2020-06-01
    • 2018-04-08
    • 2016-07-25
    • 1970-01-01
    • 2015-08-04
    • 2016-02-07
    • 2019-12-20
    相关资源
    最近更新 更多