首页 > 解决方案 > TypeError:cb不是nodejs中的函数?

问题描述

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

我正在使用护照 js 对我的网站进行身份验证,我可以获得所有路由,但是当我尝试注册后路由器时,所以在控制台中我看到这些错误,我的数据保存在数据库中,但在发布我的页面加载后不断地。这些是我得到的错误

(node:13384) UnhandledPromiseRejectionWarning: TypeError: cb is not a function at C:\Users\SBCS\Desktop\AppBlog\node_modules\passport-local-mongoose\index.js:247:59 at processTicksAndRejections (internal/process/task_queues. js:93:5)(node --trace-warnings ...用于显示警告的创建位置)(节点:13384)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志--unhandled-rejections=strict(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(拒绝 id:1)(节点:13384)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

这是我的后路由器代码

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

 })

这是我的猫鼬连接

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

谢谢

当我收到 err 节点 js 引用我这个模块代码时,请参见此处

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

}; 这是护照本地猫鼬模块代码

标签: javascriptnode.jsmongodbmongooseerror-handling

解决方案


我得到了答案

this cause by

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

一行代码,在花了更多时间之后,我得到了一些解决方案

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

另外,如果您想发送用户名,可以这样发送

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

谢谢 .....


推荐阅读