Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

node.js - Mongoose Schema Error: "Cast to string failed for value" when pushing object to empty array

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
}

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"
    } ]
});

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
});

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.

The Error says:

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

Error-Object:

   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}

Do you have an idea?

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?

What I use:

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

EDIT: 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.

EDIT 2: I added an property lastAlarm and do

device.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }
}

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

SOLUTION:

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' }
);

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...