Skip to content
Advertisement

TypeError: cb is not a function in nodejs?

(node:13384) UnhandledPromiseRejectionWarning: TypeError: cb is not a function

I am using passport js for authentication for my website, I am able to get all routes but when I try to sign up that is post router so in the console I am seeing these err, my data saved in DB but after posting my page loading continuously. here these err what I am getting

(node:13384) UnhandledPromiseRejectionWarning: TypeError: cb is not a function at C:UsersSBCSDesktopAppBlognode_modulespassport-local-mongooseindex.js:247:59 at processTicksAndRejections (internal/process/task_queues.js:93:5) (Use node --trace-warnings ... to show where the warning was created) (node:13384) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:13384) [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.

and here is my post router code

  app.post("/sign-up",(req,res)=>{

    const username = req.body.username
    const email = req.body.email
    const password = req.body.password

 User.register( {username:username}, req.body.email,req.body.password ,(err,user)=>{
     if(err){
         console.log(err);
         res.redirect("/sign-up")
        }else{
passport.authenticate("local" )(req,res, function (){
    res.redirect('/compose')
})
     }
     

 })

and here is my mongoose connection

    mongoose.connect('mongodb://localhost:27017/blog', {useNewUrlParser: true, useUnifiedTopology: true,useFindAndModify: false}).catch(err => console.log(err))
mongoose.set('useCreateIndex',true);

thanks

when I am getting err node js referring me this modules code see here

schema.statics.register = function(user, password, cb) {
// Create an instance of this in case user isn't already an instance
if (!(user instanceof this)) {
  user = new this(user);
}

const promise = Promise.resolve()
  .then(() => {
    if (!user.get(options.usernameField)) {
      throw new errors.MissingUsernameError(options.errorMessages.MissingUsernameError);
    }
  })
  .then(() => this.findByUsername(user.get(options.usernameField)))
  .then(existingUser => {
    if (existingUser) {
      throw new errors.UserExistsError(options.errorMessages.UserExistsError);
    }
  })
  .then(() => user.setPassword(password))
  .then(() => user.save());

if (!cb) {
  return promise;
}

promise.then(result => cb(null, result)).catch(err => cb(err));

}; this is passport-local-mongoose module code

Advertisement

Answer

i got answer

this cause by

User.register( {username:username}, req.body.email,req.body.password ,(err,user)=>{ if(err){

line of code and after spending more time on it,I got some solution solution is here

User.register({username: req.body.username}, req.body.password, function(err, user){

also if you want to send user name you can send it like this

User.register({username: req.body.username,name: req.body.registerName}, req.body.password, function(err, user){

thanks …..

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement