TypeError:当我解决Promise时,无法读取未定义的属性'then'

时间:2018-10-20 09:54:22

标签: node.js mongodb promise typeerror

谁能解释为什么出现错误? 我正在尝试使用凭据从数据库中获取数据,并检查DB中是否有任何与凭据相对应的数据,但是它说无法读取未定义的属性'then' 我解决了promise并添加了.catch块,但它总是说同样的话

UserSchema.statics.findByCredentials = function (email, password){
var User = this;

User.findOne({email}).then((user) => {
    if(!user) return Promise.reject();
    return new Promise((resolve, reject) => {
        bcrypt.compare(password, user.password, (err, res) => {
            if(res) {
                resolve(user);  
            }else{
                reject()
            }
        })
    })
})
};

app.post('/users/login', (req,res) => {
var body = _.pick(req.body, ['email', 'password']);

User.findByCredentials(body.email, body.password).then((user) => {
    res.send(user);
}).catch((e) =>{
    res.status(400).send();
})
});

这是我的错误详细信息

TypeError: Cannot read property 'then' of undefined
at app.post (/home/vahe/node-projects/node-todo-api/server/server.js:96:51)
at Layer.handle [as handle_request] (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
at next (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
at /home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/index.js:335:12)
at next (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/index.js:275:10)
at /home/vahe/node-projects/node-todo-api/node_modules/body-parser/lib/read.js:130:5
at invokeCallback (/home/vahe/node-projects/node-todo-api/node_modules/raw-body/index.js:224:16)
at done (/home/vahe/node-projects/node-todo-api/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/home/vahe/node-projects/node-todo-api/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1092:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:12690) 
UnhandledPromiseRejectionWarning: undefined
(node:12690) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12690) [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.
TypeError: Cannot read property 'then' of undefined
at app.post (/home/vahe/node-projects/node-todo-api/server/server.js:96:51)
at Layer.handle [as handle_request] (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
at next (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
at /home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/index.js:335:12)
at next (/home/vahe/node-projects/node-todo-api/node_modules/express/lib/router/index.js:275:10)
at /home/vahe/node-projects/node-todo-api/node_modules/body-parser/lib/read.js:130:5
at invokeCallback (/home/vahe/node-projects/node-todo-api/node_modules/raw-body/index.js:224:16)
at done (/home/vahe/node-projects/node-todo-api/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/home/vahe/node-projects/node-todo-api/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1092:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

2 个答案:

答案 0 :(得分:2)

findByCredentials不返回承诺,因此没有then方法。应该是:

UserSchema.statics.findByCredentials = function (email, password){
    var User = this;

    return User.findOne({email}).then(...)
};

答案 1 :(得分:0)

如果您不想从findByCredentials函数访问它,则应始终返回一个Promise,如下所述,

UserSchema.statics.findByCredentials = function (email, password) {
    return new Promise((resolve, reject) => {
        var User = this;
        User.findOne({ email }).then((user) => {
            if (!user) return reject();
                bcrypt.compare(password, user.password, (err, res) => {
                    if (res) {
                        resolve(user);
                    } else {
                        reject()
                    }
                })
            })
    })
}

在您之前的代码中,它的函数体findByCredentials不返回promise。 请告诉我,如果有帮助。