javascript - Update document with error: Cast to string failed for value undefined - Stack Overflow

I have a simple document with name (require), description (optional). In my model, I update a document

I have a simple document with name (require), description (optional). In my model, I update a document with a valid id and I pass description with value undefined because I want to remove this property from document. However, I got following error: message=Cast to string failed for value "undefined" at path "description", name=CastError, type=string, value=undefined, path=description . How do I remove description property on update when user does not provide 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);
    });
};

I have a simple document with name (require), description (optional). In my model, I update a document with a valid id and I pass description with value undefined because I want to remove this property from document. However, I got following error: message=Cast to string failed for value "undefined" at path "description", name=CastError, type=string, value=undefined, path=description . How do I remove description property on update when user does not provide 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);
    });
};
Share Improve this question edited Sep 17, 2013 at 20:04 Nam Nguyen asked Sep 17, 2013 at 19:46 Nam NguyenNam Nguyen 5,77014 gold badges59 silver badges73 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If someone does not want to drop down to native driver, refer to this answer https://stackoverflow./a/54320056/5947136

The issue here is using type as a key in 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
  }
});

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

Try dropping down to the native driver like so:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745212490a4616924.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信