Nodejs Promise TypeError:无法读取未定义的属性'then'

问题描述 投票:2回答:1

当我尝试在我的controller中访问route的功能时出错。我用猫鼬,快递。

错误:

TypeError:无法在/private/var/www/html/sms/server/routes/user.routes.js:70:9读取未定义的属性'then'在Layer.handle [as handle_request](/ private / var / www /html/sms/node_modules/express/lib/router/layer.js:95:5)下一步(/private/var/www/html/sms/node_modules/express/lib/router/route.js:137:13 )在Layer.dhand [/private/var/www/html/sms/node_modules/express/lib/router/route.js:112:3)在Layer.handle [as handle_request](/ private / var / www / html) /sms/node_modules/express/lib/router/layer.js:95:5)在/private/var/www/html/sms/node_modules/express/lib/router/index.js:281:22在Function.process_params (/private/var/www/html/sms/node_modules/express/lib/router/index.js:335:12)下一步(/ private / var / www / html / sms / node_modules / express / lib / router / index.js:275:10)在路由器上的Function.handle(/private/var/www/html/sms/node_modules/express/lib/router/index.js:174:3)(/ private / var / www / html / sms / node_modules / express / lib / router / index.js:47:12)在Layer.handle [as handle_request](/ private /var/www/html/sms/node_modules/express/lib/router/layer.js:95:5)trim_prefix(/private/var/www/html/sms/node_modules/express/lib/router/index.js :317:13)在/private/var/www/html/sms/node_modules/express/lib/router/index.js:284:7在Function.process_params(/ private / var / www / html / sms / node_modules /快递/ lib / router / index.js:335:12)at next(/private/var/www/html/sms/node_modules/express/lib/router/index.js:275:10)/ private / var / www / html / sms / server.js:65:3 POST / api / user / create 500 27.274 ms - 16(node:11300)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:此电子邮件已存在。请输入其他电子邮件。 (节点:11300)[DEP0018]弃用警告:不推荐使用未处理的拒绝承诺。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。

这是我的代码: -

user.cont.js

function create(data) {
  User.findOne({ email: data.email })
    .exec((err, doc) => {
      if (err) {
        return new Promise((resolve, reject) => {
          return reject(err);
        })
      } else {
        if (doc) {
          return new Promise((resolve, reject) => {
            return reject(new Error('This email is already exists. Please enter another email.'));
          })
        } else {
          const user = new User(data);
          return user.save()
        }
      }

    })

}


export default { getUsers, create };

user.route.js

router.post('/create', (req, res, next) => {
        UserCtrl.create(req.body)
            .then(savedUser => res.json(savedUser)) . // here error is generated
            .catch(e => next(e));
    });
node.js express mongoose promise
1个回答
0
投票

你应该将整个User.findOne()调用包装在Promise中,如下所示:

function create(data) {
  return new Promise((resolve, reject) => {
    User.findOne({ email: data.email })
      .exec((err, doc) => {
        if (err) return reject(err)
        if (doc) return reject(new Error('This email is already exists. Please enter another email.'))
        const user = new User(data)
        user.save((err) => {
          if (err) return reject(err)
          resolve()
        })
      })
  })
}

您还需要解析承诺,否则将永远不会调用UserCtrl.then()调用中的回调。我还添加了错误处理,以防将新的User保存到数据库时出现问题。

© www.soinside.com 2019 - 2024. All rights reserved.