Hello-World
Hello-World

Reputation: 9555

node post error using mongo

Here is my repo on github https://github.com/code-in-time/node-auth-server-from-udemy

I am trying to follow a udemy course

I am trying to signin like this. ( the account does exist ) so it should work.

POST /signin HTTP/1.1
Host: localhost:3090
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 338aea0b-3018-9185-00d9-b0affdbfaf69

{
    "email": "[email protected]",
    "password": "123"
}

Please can you help me to solve this error?

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
server listening on port, 3090
events.js:137
      throw er; // Unhandled 'error' event
      ^

ReferenceError: isMatch is not defined
    at model.userSchema.methods.comparePassword (/home/ddd/Documents/repos/node-auth-server-from-udemy/models/user.js:33:20)
    at /home/ddd/Documents/repos/node-auth-server-from-udemy/services/passport.js:17:14
    at /home/ddd/Documents/repos/node-auth-server-from-udemy/node_modules/mongoose/lib/model.js:3932:16
    at process.nextTick (/home/ddd/Documents/repos/node-auth-server-from-udemy/node_modules/mongoose/lib/query.js:2007:28)
    at process._tickCallback (internal/process/next_tick.js:150:11)
[nodemon] app crashed - waiting for file changes before startin

Upvotes: 0

Views: 39

Answers (1)

Shiven Sinha
Shiven Sinha

Reputation: 696

In this piece of code:

userSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch){
    if (err) { throw (err); }
    if (err) { return callback(err); }
});

    callback(null, isMatch);
}

Put the callback inside the function that is passed as the third argument to compare. This should be changed to

userSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch){
    if (err) { throw (err); }
    if (err) { return callback(err); }
    callback(null, isMatch);
});
}

Also, you should look to merge the two if statements with the same condition. Actually, I'm not sure if those statements do what you actually wish to achieve.

Upvotes: 1

Related Questions