Mongoose 架构错误:“Cast to string failed for value"将对象推送到空数组时 [英] Mongoose Schema Error: "Cast to string failed for value" when pushing object to empty array

查看:62
本文介绍了Mongoose 架构错误:“Cast to string failed for value"将对象推送到空数组时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,无法弄清楚问题是什么.错误消息没有帮助.

I have a strange problem and cannot figure out what the problem is. The Error-message doesn't help.

我正在发送警报"到服务器并希望将此警报保存到我的设备"已存在于数据库中.

I'm sending an "alarm" to the server and want to save this alarm to my "device" which already exist in the database.

我发送到服务器的警报对象如下所示:

The alarm object I send to the server looks like this:

{
  actionTaken: "none", 
  dateTime: "20152111191512", 
  difference: 4.88, 
  timestamp: 1448128894781
}

设备的Schema如下:

The Schema for the device is as follows:

var deviceSchema = new Schema({
   deviceId: {
        type : String,
        index : {
            unique : true,
            dropDups : true
        }
    },
    alarms : [ {
        timestamp : Number,
        dateTime : String, //yyyymmddhhss
        difference : Number,
        actionTaken : String, //"send sms"
    } ]
});

我从数据库加载设备(设置了deviceId):

I load the device from the database (deviceId is set):

Thermometer.findOne({
        deviceId : deviceId
}, function(error, device){ 
   //error handling
   var now = (new Date().getTime());
   var nowDateTime = (new Date()).toISOString().slice(0, 19).replace(/[-Ts:]/g, "");
   var newAlarm = {
       timestamp : now,
       dateTime : nowDateTime, // yyyymmddhhmmss
       difference : diff,
       actionTaken : "none"
   };
   device.alarms.push(newAlarm);  //EXCEPTION !

   //       device.save //doesn't get called
});

正如您在评论中看到的,当我想将newAlarm"对象推送到我设备的警报数组时,我收到了一个异常/错误.

As you can see in the comment, I get an Exception/Error when I want to push the "newAlarm"-object to the alarms-array of my device.

错误说:

Cast to string failed for value [object Object] at path alarms

Cast to string failed for value [object Object] at path alarms

错误对象:

   kind: "string",
   message: "Cast to string failed for value "[object Object]" at path "alarms"",
   name: "CaseError",
   path: "alarms",
   stack: undefined,
   value: {actionTaken: "none", dateTime: "20152111191512", difference: 4.88, timestamp: 1448128894781}

你有什么想法吗?

对我来说没有任何意义.数组及其内容(对象)在 Schema 中指定.为什么会出现以整个对象为值的字符串转换错误?

For me it doesn't make any sense. The array and its content (object) is specified in the Schema. Why is there a string cast error with the whole object as value?

我使用的:

"express": "3.2.6",
"express-session":"1.7.6",
"hjs": "*",
"mongoose": "4.0.5",
"nodemailer": "1.4.0"


我不想使用嵌套架构.也可以用数组来做到这一点.我用其他一些模式中的数组来做.


I don't want to use nested Schemas. It is also possible to do it with arrays. I do it with arrays in some other Schemas.

编辑 2:我添加了一个属性 lastAlarm 并执行

EDIT 2: I added an property lastAlarm and do

device.lastAlarm = alarm;

但是在那之后,thermometer.lastAlarm 仍然是未定义的……但是 alarm 是一个对象.那么设备对象是否有可能以某种方式被锁定?

but after that, thermometer.lastAlarm is still undefined... but alarm is an object. So is it possible that the device object is locked some how?

推荐答案

Mongoose 将模式中的对象与模式中的键type"解释为该对象的类型定义.

Mongoose interprets the object in the Schema with key 'type' in your schema as type definition for that object.

deviceId: {
  type : String,
  index : {
    unique : true,
    dropDups : true
    }
}

因此对于此架构,mongoose 将 deviceId 解释为 String 而不是 Object,并且不关心 deviceId 中的所有其他键.

So for this schema mongoose interprets deviceId as a String instead of Object and does not care about all other keys inside deviceId.

解决方案:

将此选项对象添加到模式声明 { typeKey: '$type' }

Add this option object to schema declaration { typeKey: '$type' }

var deviceSchema = new Schema(
{
   deviceId: {
        type : String,
        index : {
            unique : true,
            dropDups : true
        }
    },
    alarms : [ {
        timestamp : Number,
        dateTime : String, //yyyymmddhhss
        difference : Number,
        actionTaken : String, //"send sms"
    } ]
},
{ typeKey: '$type' }
);

通过添加这个,我们要求 mongoose 使用 $type 来解释键的类型而不是默认关键字 type

By adding this we are asking mongoose to use $type for interpreting the type of a key instead of the default keyword type

Mongoose 文档参考:https://mongoosejs.com/docs/guide.html#typeKey

Mongoose Docs reference: https://mongoosejs.com/docs/guide.html#typeKey

这篇关于Mongoose 架构错误:“Cast to string failed for value"将对象推送到空数组时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆