在我尝试运行代码时发生错误,但是对于我的同事来说,到目前为止还没有引发错误
我试图进行语法更改,但没有锻炼。
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/blog_demo_2", {useNewUrlParser:true});
//POST schema
var postSchema = new mongoose.Schema({
title: String,
content: String,
});
var Post = mongoose.model("Post", postSchema);
//USER schema
var userSchema = new mongoose.Schema({
name: String,
email: String,
posts: [
{
type: mongoose.Schema.Types.ObjectID,
ref: "Post"
}
]
});
var User = mongoose.model("User", userSchema);
wasif4000:〜/ workspace / associations(master)$节点reference.js
/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:414
抛出新的TypeError('模式路径' + prefix + key + '
的无效值');
^
TypeError: Invalid value for schema path `type`
at Schema.add (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:414:13)
at new Schema (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:117:10)
at Schema.interpretAsType (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:770:29)
at Schema.path (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:596:27)
at Schema.add (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:437:12)
at new Schema (/home/ubuntu/workspace/node_modules/mongoose/lib/schema.js:117:10)
at Object.<anonymous> (/home/ubuntu/workspace/associations/references.js:16:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
答案 0 :(得分:0)
您的代码中有一个逗号,请看:
var postSchema = new mongoose.Schema({
title: String,
content: String, ----> You need to get rid of it
});
这应该可以解决问题。
答案 1 :(得分:0)
更正为
type: mongoose.Schema.Types.Object.Id,
而不是ID为大写的下一个,应该为小写
type: mongoose.Schema.Types.Object*ID*,