'Received an unhandled rejection error every time I save something to my database
I keep getting this error whenever I save something to the database. Regardless if its numbers, text ect, it happens and I have no idea why. Two months prior everything worked fine and every time I saved something I got no errors, but starting a month ago this occurred:
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)
I am using Node with Mongodb Atlas and its setup with passport. Can anyone please tell me what is causing this? I forgot to mention there is no "indexOf" in the code, only located in the node modules folder which is another reason why this is strange. Running Mongoose 5.11.8, Mongoose-url-slugs 1.0.2 and Bluebird 3.7.2.
Solution 1:[1]
There is an unhanded next
object call.
Check if your middlewares or mongo hooks are missign the next param that should be called after the middleware/hook execution (it could be the pre hook call on your schema object before saving).
I had the same error a couple of years ago. Adding next
as a param of the callback fucntion might fix it
Here some example as a reference where missing the next
param of the async function will raised a similar error:
// 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);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | codetiger |