【问题标题】:sub document array saving using mongoose creates empty array MEAN stack使用 mongoose 保存子文档数组会创建空数组 MEAN 堆栈
【发布时间】: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


    【解决方案1】:

    怎么样this:

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

    【讨论】:

    • 其实我把它改成了同义词:[String],当然你的建议也是一样的。它工作正常。谢谢老哥
    猜你喜欢
    • 2015-09-03
    • 2018-11-29
    • 2016-11-26
    • 2015-08-16
    • 2013-09-20
    • 2016-03-26
    • 2020-05-15
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多