Refresh document with error: crash for line in line undefined

I have a simple document named (require), description (optional). In my model, I update the document with a valid identifier and pass the description with the value undefined because I want to remove this property from the document. However, I received the following error: message = Failed to convert to string for the value "undefined" in the path "description", name = CastError, type = string, value = undefined, path = description . How to remove the description property when updating if the user does not provide a description? Is it possible?

thanks

/*jslint indent: 2, node: true, nomen: true*/ 'use strict'; var Schema = require('mongoose').Schema; var mongoose = require('mongoose'); var mongooser = require('../../lib/mongooser'); // Schema var schema = new Schema({ name: { required: true, set: mongooser.trimSetter, trim: true, type: String, unique: true }, description: { set: mongooser.trimSetter, trim: true, type: String } }); // Export module.exports = mongoose.model('Role', schema); 

//Role.js

 var update = function (model, callback) { var test = { name: 'Users', description: undefined }; RoleSchema.findByIdAndUpdate(model.id, test, function (error, role) { callback(error, role); }); }; 
+4
source share
2 answers

Try to abandon the native driver, for example:

 var update = function (model, callback) { RoleSchema.update({_id: model.id}, {$unset: {description: 1 }}, callback); }); }; 
+1
source

If someone doesn’t want to switch to their own driver, refer to this answer fooobar.com/questions/1616878 / ...

The problem here is to use type as the key in the schema.

 var schema = new Schema({ name: { required: true, set: mongooser.trimSetter, trim: true, type: String, // <-- This is causing the issue unique: true }, description: { set: mongooser.trimSetter, trim: true, type: String // <-- This is causing the issue } }); 

See the answer above for a solution without the need for a native driver.

0
source

Source: https://habr.com/ru/post/1502672/


All Articles