1

Can anyone explain why is it trowing error? I'm trying to fetch data from database with Credentials and check if in DB are any data corresponding to credentials but it says that Cannot read property 'then' of undefined I resolved promise and added .catch block but it keep saying same thing

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();
})
});

here are my error details

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 Answers 2

2

findByCredentials doesn't return a promise, so there's no then method. It should be:

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

    return User.findOne({email}).then(...)
};
4
  • Can't understand you, I wrote exact same code here return new Promise((resolve, reject) => { bcrypt.compare(password, user.password, (err, res) => { if(res) { resolve(user); }else{ reject() } }) }) Commented Oct 20, 2018 at 10:25
  • Sorry, pasted and forgot to provide the most important part. Fixed. Commented Oct 20, 2018 at 10:28
  • sorry but I can't understand you, nothing you edited Commented Oct 20, 2018 at 10:33
  • 1
    I added return statement. findByCredentials didn't return a promise, and now it does. Commented Oct 20, 2018 at 10:37
1

If you wan't to access then from findByCredentials function it should always return a promise as mentioned below,

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()
                    }
                })
            })
    })
}

In your previous code it function body findByCredentials doesn't return promise. Please let me know,If it helps.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.