简体   繁体   English

我收到TypeError [ERR_INVALID_CALLBACK]:回调必须是一个函数

[英]I have got TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

I use express-formidable to handle the upload of the form. 我使用express-formidable来处理表单的上载。 The normal fields of the form are mounted to req.fields . 表单的普通字段将安装到req.fields

The files uploaded by the form are mounted to req.files , and the files are stored in the public/img directory. 表单上载的文件将挂载到req.files ,这些文件存储在public/img目录中。 Then the parameters are verified. 然后验证参数。

After the verification is passed, the user information is inserted into MongoDB. 验证通过后,用户信息将插入到MongoDB中。 If it succeeds, it will jump to the home page and display the notification of “registration success”. 如果成功,它将跳转到主页并显示“注册成功”的通知。 If it fails (if the user name is occupied), it will jump back to the registration page and display “The user name has been occupied" notification. 如果失败(如果用户名被占用),它将跳回到注册页面并显示“用户名已被占用”通知。

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]:回调必须是一个函数

const fs = require('fs')
const path = require('path')
const sha1 = require('sha1')
const express = require('express')
const router = express.Router()

const UserModel = require('../models/users')
const checkNotLogin = require('../middlewares/check').checkNotLogin

//Get /register The Page to Create account
router.get('/', checkNotLogin, function (req, res, next){
    res.render('register')
})

router.post('/', checkNotLogin, function(req, res, next){
     const name = req.fields.name
     const gender = req.fields.gender
     const emailaddress = req.fields.emailaddress
     const bio = req.fields.bio
     const avatar = req.files.avatar.path.split(path.sep).pop()
     let password = req.fields.password
     const repassword = req.fields.repassword

     try {
         if (!(name.length >= 1 && name.length <= 10)) {
           throw new Error('Username can not be longer than 10 characters')
         }
         if (['m', 'f', 'x'].indexOf(gender) === -1) {
           throw new Error('Gender only can be m、f or x')
         }
         if(!emailaddress.endsWith('.co.uk')){
             throw new Error('Email Address should be an academic address')
         }
         if (!(bio.length >= 1 && bio.length <= 30)) {
           throw new Error('Please organise your personal description with in 30 characters')
         }
         if (!req.files.avatar.name) {
           throw new Error('Please choose your avator')
         }
         if (password.length < 6) {
           throw new Error('Password should be longer than 6 characters')
         }
         if (password !== repassword) {
           throw new Error('Re-enter the password you set')
         }
       } catch (e) {
         // register fail, delete the avator
         fs.unlink(req.files.avatar.path)
         req.flash('error', e.message)
         return res.redirect('/register')
       }

       // text password encryption
       password = sha1(password)

       // information wait to be enter into database
       let user = {
         name: name,
         password: password,
         emailaddress:emailaddress,
         gender: gender,
         bio: bio,
         avatar: avatar
       }
       // enter user information into database
       UserModel.create(user)
         .then( function (result) {
           user = result.ops[0]
           // delete password,store user information into session
           delete user.password
           req.session.user = user
           //Write into flash
            req.flash('success', 'Account Successfully created')
           // Jump to the home
           res.redirect('/posts')
          })
         .catch(function (e) {
           // Account create failure, delete avator
            fs.unlink(req.files.avatar.path)
           // if username already been use return to register page
           if (e.message.match('duplicate key')) {
             req.flash('error', 'username already be taken')
             return res.redirect('/register')
           }
          next(e)
         })
      })

     module.exports = router

I am not sure where is going wrong I think there are already funcition Any help will be appreciate 我不确定哪里出了问题我认为已经有功能了,将不胜感激

This is full error message: 这是完整的错误消息:

 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
 at makeCallback (fs.js:137:11)
 at Object.unlink (fs.js:936:14)
 at D:\tttt\CVsWeb\routes\register.js:48:1 express\lib\router\layer.js:95:5)
 at Layer.handle [as handle_request] (D:\tttt\CVsWeb\node_modules\137:13 express\lib\router\layer.js:95:5)
 at next        (D:\tttt\CVsWeb\node_modules\express\lib\router\route.js:express\lib\router\layer.js:95:5)137:13)                                                              137:13)
 at checkNotLogin (D:\tttt\CVsWeb\middlewares\check.js:14:9)      \route.js:112:3)
 at Layer.handle [as handle_request(D:\tttt\CVsWeb\node_modules\express\lib\router\layer.js:95:5)express\lib\router\layer.js:95:5)
at next (D:\tttt\CVsWeb\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\tttt\CVsWeb\node_modules\express\lib\router\route.js:112:3)
  at Layer.handle [as handle_request] (D:\tttt\CVsWeb\node_modules\express\lib\router\layer.js:95:5)

fs.unlink is asynchronous and requires a callback function to be passed as it's second parameter. fs.unlink是异步的,需要将回调函数作为第二个参数传递。

Try using fs.unlinkSync(req.files.avatar.path) or insert a function as the second parameter to your unlink fs.unlink(req.files.avatar.path, function(){console.log('Deleted avatar')}) 尝试使用fs.unlinkSync(req.files.avatar.path)或将函数作为第二个参数插入到unlink fs.unlink(req.files.avatar.path, function(){console.log('Deleted avatar')})

router.post('/', checkNotLogin, function(req, res, next) {
  const name = req.fields.name
  const gender = req.fields.gender
  const emailaddress = req.fields.emailaddress
  const bio = req.fields.bio
  const avatar = req.files.avatar.path.split(path.sep).pop()
  let password = req.fields.password
  const repassword = req.fields.repassword

  try {
    if (!(name.length >= 1 && name.length <= 10)) {
      throw new Error('Username can not be longer than 10 characters')
    }
    if (['m', 'f', 'x'].indexOf(gender) === -1) {
      throw new Error('Gender only can be m、f or x')
    }
    if (!emailaddress.endsWith('.co.uk')) {
      throw new Error('Email Address should be an academic address')
    }
    if (!(bio.length >= 1 && bio.length <= 30)) {
      throw new Error('Please organise your personal description with in 30 characters')
    }
    if (!req.files.avatar.name) {
      throw new Error('Please choose your avator')
    }
    if (password.length < 6) {
      throw new Error('Password should be longer than 6 characters')
    }
    if (password !== repassword) {
      throw new Error('Re-enter the password you set')
    }
  } catch (e) {
    // register fail, delete the avator
    fs.unlinkSync(req.files.avatar.path) //Try this!
    req.flash('error', e.message)
    return res.redirect('/register')
  }

  // text password encryption
  password = sha1(password)

  // information wait to be enter into database
  let user = {
    name: name,
    password: password,
    emailaddress: emailaddress,
    gender: gender,
    bio: bio,
    avatar: avatar
  }
  // enter user information into database
  UserModel.create(user)
    .then(function(result) {
      user = result.ops[0]
      // delete password,store user information into session
      delete user.password
      req.session.user = user
      //Write into flash
      req.flash('success', 'Account Successfully created')
      // Jump to the home
      res.redirect('/posts')
    })
    .catch(function(e) {
      // Account create failure, delete avator
      fs.unlinkSync(req.files.avatar.path) //Also this one
      // if username already been use return to register page
      if (e.message.match('duplicate key')) {
        req.flash('error', 'username already be taken')
        return res.redirect('/register')
      }
      next(e)
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError [ERR_INVALID_CALLBACK]: 回调必须是一个函数 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]:回调必须是Express JS函数 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function Express JS 如何修复此错误 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function - how to fix this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]:回调必须是 function。 在nodejs中收到未定义 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined in nodejs TypeError [ERR_INVALID_CALLBACK]中理解中的问题:回调必须是节点中的函数 - issue in understanding in `TypeError [ERR_INVALID_CALLBACK]: Callback must be a function` in node TypeError [ERR_INVALID_CALLBACK]: 回调必须是 function。接收未定义 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined Maven ERR_INVALID_CALLBACK( - Maven ERR_INVALID_CALLBACK( express --view=hbs myapp getting [ERR_INVALID_CALLBACK]: Callback must be a function - express --view=hbs myapp getting [ERR_INVALID_CALLBACK]: Callback must be a function [ERR_INVALID_CALLBACK]:使用 fs.writeFile 时,回调必须是 function - [ERR_INVALID_CALLBACK]: Callback must be a function when using fs.writeFile 抛出新的 ERR_INVALID_CALLBACK(回调); - throw new ERR_INVALID_CALLBACK(callback);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM