Update document with error: Cast to string failed for value undefined

前端 未结 2 1066
半阙折子戏
半阙折子戏 2021-01-13 22:03

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

相关标签:
2条回答
  • 2021-01-13 22:16

    If someone does not want to drop down to native driver, refer to this answer https://stackoverflow.com/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.

    0 讨论(0)
  • 2021-01-13 22:33

    Try dropping down to the native driver like so:

    var update = function (model, callback) {
       RoleSchema.update({_id: model.id}, {$unset: {description: 1 }}, callback);
       });
    };
    
    0 讨论(0)
提交回复
热议问题