CastError:投射到ObjectId失败的值“types:”缓冲区“”

试图创build一个新的用户在我的Web应用程序没有意识到已经有一个用户在我的数据库用户名相同。 出于某种原因,这个边界案例打破了我的应用程序,现在每次我尝试去主页,我得到:

(节点:14649)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):CastError:强制转换为ObjectId失败的值为“{type:'Buffer',data:[89,243,30,231,66,69,45, (节点:14649)[DEP0018]弃用警告:不处理的承诺拒绝已被弃用。 将来,未处理的承诺拒绝将使用非零退出代码来终止Node.js进程。

是什么使这个问题独特的从这里的其他问题在stackoverflow是事实,types是'缓冲区'。 没有其他问题有这个具体问题。 我不知道如何创build一个缓冲区,当我没有在我的数据库或节点服务器中使用它们。

这是在缓冲区错误之前发生的重复错误消息:

{MongoError:E11000重复键错误集合:crud2.users索引:username_1 dup键:{:“lightbeam”}在Function.MongoError.create(/ Users / * /桌面/编码/应用程序/ thequoteapp / node_modules / mongodb核心/在/ Users / * / Desktop / coding / lib目录下的toError(/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/utils.js:139:22)中的lib / error.js:31:11) apps / thequoteapp / node_modules / mongodb / lib / collection.js:668:23 at handleCallback(/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/utils.js:120:56)at / Users /*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/bulk/unordered.js:465:9 at handleCallback(/ Users / * / Desktop / coding / apps / thequoteapp / node_modules / mongodb / lib / utils。 (/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/bulk/unordered.js:413:5)at / Users / * / Desktop / coding / apps / js:120:56)在process.tickCa上的_combinedTickCallback(internal / process / next_tick.js:131:7)处的thequoteapp / node_modules / mongodb-core / lib / connection / pool.js:469:18 llback(internal / process / next_tick.js:180:9)name:'MongoError',message:'E11000 duplicate key error collection:crud2.users index:username_1 dup key:{:“lightbeam”}',driver:true,代码:11000,索引:0,errmsg:'E11000重复键错误集合:crud2.users索引:username_1 dup键:{:“lightbeam”}“,getOperation:[Function],toJSON:[Function],toString:[Function ]}

这是我的用户架构:

const express = require('express') const mongoose = require('mongoose') const Schema = mongoose.Schema let UserSchema = new Schema ({ username: { type: String, unique: [true, "This username is taken."], trim: true }, email: { type: String, unique: [true, "This email is already being used."], trim: true }, password: { type: String, trim: true } }, { timestamps: true, favorites: [] }) // first parameter is the name of the collection! If does not exist, will be created! const User = mongoose.model('users', UserSchema) module.exports = User 

这里是我的route.js文件中定义的get请求:

 /* route handling for HOME PAGE. */ router.get('/', (req, res, next) => { console.log("req.user value: ", req.user) Quote.find({}) .exec((err, results) => { err ? console.log(err) : res.render('index', { quotes: results }) }) }) 

最后发出创build新用户的请求:

 router.post('/signup/users', (req, res, next) => { req.checkBody('username', 'Username field cannot be empty.').notEmpty() req.checkBody('username', 'Username must be between 4-30 characters long.').len(4, 30) req.checkBody('email', 'The email you entered is invalid, please try again.').isEmail() req.checkBody('email', 'Email address must be between 4-100 characters long, please try again.').len(4, 100) req.checkBody('password', 'Password must be between 8-100 characters long.').len(8, 100) // req.checkBody('password', 'Password must include one lowercase character, one uppercase character, a number, and a special character.').matches(/^(?=.*\d)(?=.*[az])(?=.*[AZ])(?!.* )(?=.*[^a-zA-Z0-9]).{8,}$/, 'i') req.checkBody('passwordMatch', 'Password must be between 8-100 characters long.').len(8, 100) req.checkBody('passwordMatch', 'Passwords do not match, please try again.').equals(req.body.password) // Additional validation to ensure username is alphanumeric with underscores and dashes req.checkBody('username', 'Username can only contain letters, numbers, or underscores.').matches(/^[A-Za-z0-9_-]+$/, 'i') const errors = req.validationErrors() if(errors) { res.render('signup', { errors: errors }) } else { let password = req.body.password bcrypt.hash(password, saltRounds, (err, hash) => { user = new User() user.username = req.body.username user.email = req.body.email user.password = hash user.save((err, result) => { if(err) { console.log(err) } else { User.find({}).sort({ _id:-1 }).limit(1) .exec((err, newuser) => { if (err) { console.log(err) } else { userId = ObjectId(newuser.id) // logins user through passport function req.login(userId, (err) => { if (err) { console.log("Login error: " + err) } else { res.redirect('/home') } }) } }) .catch(next) } }) }) } }) 

我已经尝试重新启动nodemon和mongodb服务器,并试图使用:

 kill -2 `pgrep mongo` 

清除mongodb服务器的任何和所有实例。 请帮忙!