我是nodejs和mongoose的新手。有mongoose架构声明的问题。
我声明了一个架构如下:
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我想保存:
{"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"}
]
]
}
整个计划:
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)
请让我知道我做错了什么。我用Google搜索并找不到任何有助于我理解我的代码错误的内容。
提前致谢!!!
答案 0 :(得分:0)
要将数据作为JSON字符串提供给new routes(...)
,您必须首先解析它:
var allroutes = new routes(JSON.parse('{"mode": []}'));
routes()
实现的Model
constructor期望JavaScript Object
的参数而不是JSON字符串。
<强>参数:强>
doc
&lt; Object&gt;用于创建文档的值
您还可以从参数中删除周围的引号,以使用initializers定义JavaScript对象和数组(JSON从中获取了一些语法):
var allroutes = new routes({"mode": []});
答案 1 :(得分:0)
我创建了这个代码(你的一个淡化版本),并且可以使用:node test.js
调用它test.js
#!/bin/env node
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var smode = new Schema({ name: String });
var routes = mongoose.model('route', smode);
var allroutes = new routes({ name: 'My first route' });
allroutes.save(function (err) {
if (err) return handleError(err);
routes.findById(allroutes, function (err, doc) {
if (err) return handleError(err);
console.log(doc);
})
})
您可以尝试一步一步地将其重新构建回原来的版本,或者至少验证您的语法。请注意,在该行&#34; var allroutes ...&#34;例如,单词名称不包含引号。