我现在已经创建了架构我想插入到mongodb集合中,但是它的抛出错误diagramModel.insert
没有定义什么是错误的实现?
app.js
mongoose.connect('mongodb://localhost:27017/develop-modeler');
require('./server/api/diagram/diagram.model.js');
var diagramModel = mongoose.model('Diagram');
var newDiagram = {
"owner" : "sh529u",
"text" : "sco_poc.bpmn",
"users":["wp6307","kz323j","ew6980"],
"groups":[],
"string" : "test"
}
mongoose.connection.on('connected', function () {
diagramModel.insert(newDiagram,function(err,res){
if (err) { console.log(err);}
else {
diagramModel.find({}, function(err, data) { console.log(data);});
}
});
});
diagram.model.js
var DiagramSchema = new mongoose.Schema({
text: String,
owner: {type: String, ref:'User'},
groups: [{type: String, ref: 'Group'}],
users: [{type: String, ref: 'User'}],
string: String
});
mongoose.model('Diagram', DiagramSchema);
答案 0 :(得分:1)
我认为保存记录时save
不是insert
。这就是你收到此错误的原因
mongoose.connect('mongodb://localhost:27017/develop-modeler');
var diagramModel = require('./server/api/diagram/diagram.model.js');
var newDiagram = {
"owner": "sh529u",
"text": "sco_poc.bpmn",
"users": ["wp6307", "kz323j", "ew6980"],
"groups": [],
"string": "test"
}
mongoose.connection.on('connected', function() {
diagramModel.save(newDiagram, function(err, res) {
if (err) {
console.log(err);
} else {
diagramModel.find({}, function(err, data) {
console.log(data);
});
}
});
});
var DiagramSchema = mongoose.Schema({
text: String,
owner: {type: String, ref:'User'},
groups: [{type: String, ref: 'Group'}],
users: [{type: String, ref: 'User'}],
string: String
});
module.exports=mongoose.model('Diagram', DiagramSchema);
答案 1 :(得分:0)
试试这个完美的工作
var diagramModel = require('./server/api/diagram/diagram.model.js');
var finalDiagram = diagramModel({
"owner" : "sh529u",
"text" : "sco_poc.bpmn",
"users":["wp6307","kz323j","ew6980"],
"groups":[],
"string" : "test"
});
mongoose.connection.on('connected', function () {
finalDiagram.save(function (err, dataObj) {
if (err) { console.log(err);}
} else {
console.log("DATA",dataObj);
diagramModel.find({}, function(err, data) { console.log(data);});
}
});
});
答案 2 :(得分:0)
创建模型的新实例,此实例包含save
方法:
mongoose.connect('mongodb://localhost:27017/develop-modeler');
require('./server/api/diagram/diagram.model.js');
var diagramModel = mongoose.model('Diagram');
var newDiagram = new diagramModel({
"owner" : "sh529u",
"text" : "sco_poc.bpmn",
"users":["wp6307","kz323j","ew6980"],
"groups":[],
"string" : "test"
});
mongoose.connection.on('connected', function () {
newDiagram.save(function(err,res){
if (err) { console.log(err);}
else {
diagramModel.find({}, function(err, data) { console.log(data);});
}
});
});