每次将内容保存到数据库时,都会收到一个未处理的拒绝错误



每当我将某些内容保存到数据库时,我都会不断收到这个错误。不管它的数字,文本等等,它都会发生,我不知道为什么。两个月前,一切都很好,每次我保存东西时都没有错误,但从一个月前开始,出现了这种情况:

Unhandled rejection TypeError: Cannot read property 'indexOf' of undefined
at model.isSelected (/var/www/html/node_modules/mongoose/lib/document.js:2056:12)
at model.<anonymous> (/var/www/html/node_modules/mongoose-url-slugs/index.js:215:18)
at callMiddlewareFunction (/var/www/html/node_modules/kareem/index.js:483:23)
at next (/var/www/html/node_modules/kareem/index.js:58:7)
at Kareem.execPre (/var/www/html/node_modules/kareem/index.js:87:8)
at Kareem.wrap (/var/www/html/node_modules/kareem/index.js:266:8)
at model.$__validate (/var/www/html/node_modules/kareem/index.js:376:11)
at /var/www/html/node_modules/mongoose/lib/document.js:2226:10
at promiseOrCallback (/var/www/html/node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
at model.Document.validate (/var/www/html/node_modules/mongoose/lib/document.js:2221:10)
at model.validateBeforeSave (/var/www/html/node_modules/mongoose/lib/plugins/validateBeforeSave.js:35:12)
at callMiddlewareFunction (/var/www/html/node_modules/kareem/index.js:483:23)
at next (/var/www/html/node_modules/kareem/index.js:58:7)
at Kareem.execPre (/var/www/html/node_modules/kareem/index.js:87:8)
at Kareem.wrap (/var/www/html/node_modules/kareem/index.js:266:8)
at model.$__save (/var/www/html/node_modules/kareem/index.js:376:11)
at /var/www/html/node_modules/mongoose/lib/model.js:492:10
at /var/www/html/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
at Promise._execute (/var/www/html/node_modules/bluebird/js/release/debuggability.js:384:9)
at Promise._resolveFromExecutor (/var/www/html/node_modules/bluebird/js/release/promise.js:518:18)
at new Promise (/var/www/html/node_modules/bluebird/js/release/promise.js:103:10)
at promiseOrCallback (/var/www/html/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)

我将Node与Mongodb Atlas一起使用,并使用passport进行设置。有人能告诉我是什么原因造成的吗?我忘了提没有";indexOf";在代码中,只位于nodemodules文件夹中,这也是为什么这很奇怪的另一个原因。运行Mongoose 5.11.8、Mongoose url slugs 1.0.2和Bluebird 3.7.2。

有一个未处理的next对象调用。

检查您的中间件或mongo钩子是否缺少在中间件/钩子执行之后应该调用的下一个参数(它可能是保存之前对架构对象的预钩子调用(。

几年前我也犯过同样的错误。添加next作为回调函数的参数可能会修复

这里有一个例子作为参考,其中缺少异步函数的next参数将引发类似的错误:

// user.model.js
userScheme.pre('save', async function (next) { 
const user = this;                                       

try {                                                    
if (!user.isModified('password')) {                    
return next();                                       
}                                                      
// generates the salt to encrypt the password          
const salt = await bcrypt.genSalt(10);                 
// generate hashed password 
const hash = await bcrypt.hash(user.password, salt); 
// this is the hashed password that we are going to save in the DB 
user.password = hash; 
} catch (err) { 
next(err); 
} 

});

相关内容

最新更新