Mongoose架构错误:将对象推向空数组时,“Cast to string failed for value”

我有一个奇怪的问题,不知道是什么问题。 错误消息没有帮助。

我正在向服务器发送一个“警报”,并希望将此警报保存到数据库中已存在的“设备”中。

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

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

该设备的架构如下:

 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设置):

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

正如您在评论中看到的那样,当我要将“newAlarm” – 对象推送到设备的alarm-array时,我得到一个Exception / Error。

错误说:铸造string失败的价值“[对象对象]”path“报警”

错误对象:

  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中指定。 为什么有整个对象作为值的string转换错误?

我使用的是:

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

编辑:我不想使用嵌套的架构。 也可以用数组来完成。 我在一些其他模式中使用数组。

编辑2 :我添加了一个属性“lastAlarm”,并做

 device.lastAlarm = alarm; 

但之后,温度计。popup报警仍然是未定义的…但报警是一个对象。 那么有可能是设备对象被locking了一些如何?

我会声明警报作为自己的模式,并设置警报属性为警报又名子文档数组。 这将允许您将validation添加到警报模式等。 注意 :保存父项之前,子文档不会保存。

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

我会build议为报警制定一个自己的模式。 我想你不能像你一样在一个模式中定义一个数组。

使用内部模式来解决这个问题,

 var SchemaObject = require('node-schema-object'); // Create custom basic type // Type can be extended with more properties when defined var NotEmptyString = {type: String, minLength: 1}; // Create sub-schema for user's Company var Company = new SchemaObject({ startDate: Date, endDate: Date, name: NotEmptyString }); // Create User schema var User = new SchemaObject({ // Basic user information using custom type firstName: NotEmptyString, lastName: NotEmptyString, // "NotEmptyString" with only possible values as 'm' or 'f' gender: {type: NotEmptyString, enum: ['m', 'f']}, // Index with sub-schema company: Company, // An array of Objects with an enforced type workHistory: [Company], // Create field which reflects other values but can't be directly modified fullName: {type: String, readOnly: true, default: function() { return (this.firstName + ' ' + this.lastName).trim(); }} }); // Initialize a new instance of the User with a value var user = new User({firstName: 'Scott', lastName: 'Hovestadt', gender: 'm'}); // Set company name user.company.name = 'My Company'; // The date is automatically typecast from String user.company.startDate = 'June 1, 2010'; // Add company to work history user.workHistory.push({ name: 'Old Company', startDate: '01/12/2005', endDate: '01/20/2010' }); console.log(user.toObject()); // Prints: { firstName: 'Scott', lastName: 'Hovestadt', gender: 'm', company: { startDate: Tue Jun 01 2010 00:00:00 GMT-0700 (PDT), endDate: undefined, name: 'My Company' }, workHistory: [ { startDate: Wed Jan 12 2005 00:00:00 GMT-0800 (PST), endDate: Wed Jan 20 2010 00:00:00 GMT-0800 (PST), name: 'Old Company' } ], fullName: 'Scott Hovestadt' }