【发布时间】:2018-03-20 11:57:59
【问题描述】:
我对 mongo db 和 mongoose 很陌生。尝试使用子文档数组保存架构时遇到问题。子文档保存为空白。 我尝试在网上阅读许多解决方案,但找不到/确定合适的解决方案,因为我对此很陌生。
这是我的模式 js 和路由器 js 的代码 sn-p,
schema.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const AppEntitySchema1 = new Schema({
name : {type:String, unique:true},
appName: String,
entries : [{
value : String,
synonyms:[{String}]
}]
});
const AppEntitySchema = mongoose.model('entity',AppEntitySchema1);
module.exports = AppEntitySchema;
router.js
// load the Entity model
var Entity = require('../models/entity');
// expose the routes to our app with module.exports
module.exports = function(app) {
// create entity and send back all entities after creation
app.post('/api/entities', function(req, res) {
let model = new Entity(req.body);
model.save(function(err,createdObject){
if(err){
res.send(err);
return;
}
res.send(createdObject);
})
});
}
【问题讨论】:
标签: node.js mongodb mongoose mean-stack mongoose-schema