当我尝试在我的route
中访问controller
的功能时出现错误。我用 Mongoose , express 。
错误:
TypeError: Cannot read property 'then' of undefined at /private/var/www/html/sms/server/routes/user.routes.js:70:9 at Layer.handle [as handle_request] (/private/var/www/html/sms/node_modules/express/lib/router/layer.js:95:5) at next (/private/var/www/html/sms/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/private/var/www/html/sms/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/private/var/www/html/sms/node_modules/express/lib/router/layer.js:95:5) at /private/var/www/html/sms/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/private/var/www/html/sms/node_modules/express/lib/router/index.js:335:12) at next (/private/var/www/html/sms/node_modules/express/lib/router/index.js:275:10) at Function.handle (/private/var/www/html/sms/node_modules/express/lib/router/index.js:174:3) at router (/private/var/www/html/sms/node_modules/express/lib/router/index.js:47:12) at Layer.handle [as handle_request] (/private/var/www/html/sms/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/private/var/www/html/sms/node_modules/express/lib/router/index.js:317:13) at /private/var/www/html/sms/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/private/var/www/html/sms/node_modules/express/lib/router/index.js:335:12) at next (/private/var/www/html/sms/node_modules/express/lib/router/index.js:275:10) at /private/var/www/html/sms/server.js:65:3 POST /api/user/create 500 27.274 ms - 16 (node:11300) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: This email is already exists. Please enter another email. (node:11300) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
这是我的代码:-
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));
});
最佳答案
您应该将整个 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()
})
})
})
}
您还需要解决 promise ,否则您的 UserCtrl.then()
调用中的回调将永远不会被调用。我还添加了错误处理,以防将新的 User
保存到数据库时出现问题。
关于node.js - Nodejs promise 类型错误 : Cannot read property 'then' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47903574/