简体   繁体   English

调用 controller function 与 promise 无法等待,然后 ZC1C425268E68385D1AB50741C

[英]calling a controller function with promise unable to await with then function

I have a function in a controller that wants to create two different models if a condition is satisfied in the data base.如果数据库中满足条件,我在 controller 中有一个 function 想要创建两个不同的模型。 The API gets called: API 被调用:

PUT localhost:3000/user PUT 本地主机:3000/用户

calls app.post('/user', user.create)调用 app.post('/user', user.create)

the user.create is in a controller file: user.create 在 controller 文件中:

const business = require("./controllers/business.controller.js");

exports.create = (req,res) => {

    const user = new User ({
    email: req.body.email
    uniqueAccount: req.body.unique
    })

    User.create (user, function(err,doc){
        if (err) res.status(500).send(err)
        if (user.uniqueAccount) {
            let result = business.create(req,res)
            console.log(result)
        }

        res.send(doc)
    })
}

in the business controller, it is the same function:在业务 controller 中,同样是 function:

exports.create = (req,res) => {
    const business = new Business({
        name: req.body.businessname
    })

    Business.create (business, function(err,doc){
        if (err) return err
        return doc
    }
}

because my of the line:因为我的行:

let result = business.create(req,res) has no promise await, I am not able to see return from the business controller. let result = business.create(req,res)没有 promise 等待,我无法看到来自业务 controller 的返回。 I tried using a.then function but I come accross the error:我尝试使用 a.then function 但我遇到了错误:

TypeError: Cannot read property 'then' of undefined TypeError:无法读取未定义的属性“then”

How do I properly go about this?我该如何正确 go 关于这个? I could just import the model into my user controller, and then create the business model in the user controller but I wanted to separate the purposes. I could just import the model into my user controller, and then create the business model in the user controller but I wanted to separate the purposes.

You can use await on a function only if it returns a promise, so return a promise from your business controller instead of returning a simple value.仅当 function 返回 promise 时,您才可以在 function 上使用 await,因此请从您的业务 Z594C103F2C6E0E4C3D 中返回 promise 而不是返回简单值 promise。

You can promisify your business controller function and then use await inside your user.create function.您可以承诺您的业务 controller function 然后在您的 user.create function 中使用 await。 Do it like this:像这样做:

     const business = require("./controllers/business.controller.js");

     exports.create = (req,res) => {

        const user = new User ({
        email: req.body.email
        uniqueAccount: req.body.unique
     })

     User.create (user, async function(err,doc){
       if (err) res.status(500).send(err)
       if (user.uniqueAccount) {
         let result = await business.create(req,res)
         console.log(result)
       }

       res.send(doc)
      })
    }

Now promisify your business controller function.现在承诺您的业务 controller function。

      exports.create = (req,res) => {
        return new Promise((resolve,reject)=>{
          const business = new Business({
            name: req.body.businessname
          })

          Business.create (business, function(err,doc){
             if (err) reject(err)
             resolve(doc)
          }
        });
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM