验证错误Mongodb

时间:2016-11-25 13:39:00

标签: javascript mongodb validation typescript mongoose

我正面临这个奇怪的问题,它给了我某个架构的ValidationError。我不确定崩溃的原因,因为对象的类型对我来说似乎完全没问题。

// works fine but the Data in ObservationsObject is LOST

var ObservationsObject = Observations.ObservationsModel(timeTable.learningCenter.observations);
var LearningCenterObject = LearningCenter.LearningCenterModel(timeTable.learningCenter);
LearningCenterObject.observations = ObservationsObject;



 // GIVES VALID ERROR UPON LEARNINGCENTER.SAVE()

var LearningCenterObject = LearningCenter.LearningCenterModel(timeTable.learningCenter);
for (var i = 0; i < timeTable.learningCenter.observations.length; i++) {
    var ObservationsObject = Observations.ObservationsModel(timeTable.learningCenter.observations[i]);
    LearningCenterObject.observations.push(ObservationsObject);
}

保存代码:

LearningCenterObject.save(function (err, learningCenterSavedObject) {
        if (err) {
            console.error("TimeTableController->LearningCenterObject->save()", err);
            return;
        }
        console.log("TimeTableController->LearningCenterObject->save() SUCCESS");

        ObservationsObject.save(function (err, observationSavedObject) {
            if (err) {
                console.error("TimeTableController->LearningCenterObject->Observations->save()", err);
                return;
            }

            console.log("TimeTableController->LearningCenterObject->Observations->save() SUCCESS");
        });
    });

架构:

learningCenterSchema = new mongoose.Schema({
        name : String,
        observations: [{type: Schema.Types.ObjectId, ref: "observations_model"}]
    });


observationsSchema = new mongoose.Schema({
        obsNum : String,
        data: {ageGroup : String, category: String, description: String, tips: String}
    });

错误:

TimeTableController->LearningCenterObject->save() { [ValidationError: learning_center_model validation failed]
  message: 'learning_center_model validation failed',
  name: 'ValidationError',
  errors: 
   { observations: 
      { [CastError: Cast to Array failed for value "[object Object]" at path "observations"]
        message: 'Cast to Array failed for value "[object Object]" at path "observations"',
        name: 'CastError',
        kind: 'Array',
        value: [Object],
        path: 'observations',
        reason: [Object] } } }

SAMPLE JSON:

"learningCenter": {
    "name": "asdf",
    "observations": [
      {
        "obsNum": "1.1111",
        "data": {
          "ageGroup": "sadf",
          "category": "df",
          "description": "adf",
          "tips": "asdf"
        }
      }
    ]
}

3 个答案:

答案 0 :(得分:1)

对于新手而言,错误有点神秘和模糊。用外行的话来说,错误就是

&#34;您正在将一个OBJECT OBJECT数组的对象分配给OBJECT,该对象只能接受包含OBJECTID&#34;的OBJECT的引用。

虽然我在数组中推送的对象是{object,object}(不确定为什么Mongoose会说它[对象对象],可能是一个更精通的人可以帮我解决这个问题)对象这是一个ARRAY OF OBJECT ID,猫鼬验证失败了。

为了解决这个问题,我创建了一个数组。推送ObservationModel对象在该数组中对象,并将该数组分配给LearningCenter.observations。

var myArray = new Array();

var LearningCenterObject = LearningCenter.LearningCenterModel(timeTable.learningCenter);
for (var i = 0; i < timeTable.learningCenter.observations.length; i++) {
    var ObservationsObject = Observations.ObservationsModel(timeTable.learningCenter.observations[i]);
    myArray.push(ObservationsObject);
}

LearningCenterObject.observations = myArray;

答案 1 :(得分:0)

我在尝试更新数组中没有任何内容的模型时遇到了同样的问题。如果数组中没有任何内容,我只需将其初始化为空数组。

    if (contact.group && contact.group.length > 0){
        for (let groupContact of contact.group) {
            groupContact = groupContact._id;
         }
    } else {
       contact.group = [];
    }

... contact.save

答案 2 :(得分:0)

得到相同的错误,并找出解决方案。对我来说,问题是我使用default: []作为参考子文档,例如product: { type: productSchema, default: [] }

我删除了default: [],并修复了错误。