dimanche 29 mars 2015

unable to save JSON through declared mongoosejs schema

I am new to nodejs and mongoose. Have issue with mongoose schema declaration.


I declared a schema as below:



var srouteDetails = new Schema({route_id:String, route_name:String});
var srouteTypes = new Schema({route_type:String, mode_name:String, route:[srouteDetails]});
var smode = new Schema({mode:[srouteTypes]});


JSON I am trying to save:



{"mode":
[
"route_type":"4",
"mode_name":"Boat",
"route": [
{"route_id":"Boat-F4","route_name":"Charlestown Ferry"},
{"route_id":"Boat-F1","route_name":"Hingham Ferry"},
{"route_id":"Boat-F3","route_name":"Hull Ferry"}
]
]
}


Whole program:



var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var srouteDetails = new Schema({route_id:String, route_name:String});
var srouteTypes = new Schema({route_type:String, mode_name:String, route:[srouteDetails]});
var smode = new Schema({mode:[srouteTypes]});

mongoose.connect('mongodb://localhost/test');

var routes = mongoose.model('route',smode);

var allroutes = new routes('{"mode": [{"route_type": "4","mode_name": "Boat","route": [{"route_id": "Boat-F4","route_name": "Charlestown Ferry"},{"route_id": "Boat-F1","route_name": "Hingham Ferry"},{"route_id": "Boat-F3","route_name": "Hull Ferry"}]}]}');

allroutes.save(function(err){
if(err) throw err;
console.log("Saved");
});


ERROR:



/Workspace_Js/jsontest/node_modules/mongoose/lib/document.js:158
if (obj && '_id' in obj) continue;
^
TypeError: Cannot use 'in' operator to search for '_id' in {"mode": [{"route_type": "4","mode_name": "Boat","route": [{"route_id": "Boat-F4","route_name": "Charlestown Ferry"},{"route_id": "Boat-F1","route_name": "Hingham Ferry"},{"route_id": "Boat-F3","route_name": "Hull Ferry"}]}]}
at model.Document.$__buildDoc (/Users/Sravan/Workspace_Js/jsontest/node_modules/mongoose/lib/document.js:158:27)
at model.Document (/Users/Sravan/Workspace_Js/jsontest/node_modules/mongoose/lib/document.js:57:20)
at model.Model (/Users/Sravan/Workspace_Js/jsontest/node_modules/mongoose/lib/model.js:43:12)
at new model (/Users/Sravan/Workspace_Js/jsontest/node_modules/mongoose/lib/model.js:2536:11)
at Object.<anonymous> (/Users/Sravan/Workspace_Js/jsontest/index.js:12:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)


Please let me know what I am doing wrong.I googled and cant find anything that helped me understand whats wrong with my code.


Thanks in advance!!!


Aucun commentaire:

Enregistrer un commentaire