使用mongoose保存子文档数组创建空数组MEAN堆栈

时间:2017-10-09 09:56:57

标签: node.js mongodb mongoose mean-stack mongoose-schema

我是mongo db和mongoose的新手。尝试使用子文档数组保存模式时遇到问题。子文档保存为空白。 我尝试在网上阅读许多解决方案但无法找到/识别合适的解决方案,因为我对此很陌生。

这是我的架构js和路由器js的代码片段,

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);
        })
    });
}

Output : 在此先感谢!!

1 个答案:

答案 0 :(得分:0)

如何做this

const AppEntitySchema1 = new Schema({
    name : {type:String, unique:true},
    appName: String,
    entries : [{
        value : String,
        synonyms:[{
          type: String
      }]
    }]
});
相关问题