【问题标题】:How to save array of objects in mongoose?如何在猫鼬中保存对象数组?
【发布时间】:2021-04-02 00:50:53
【问题描述】:

嗨,我有一个看起来像这样的对象数组

[{
             "id": 0,
             "name": "Del Vecchios | Italian Food | Asheville, NC",
             "domain": "delvecchiositalian.com",
             "email": "eat@delvecchiositalian.com",
             "phone": "828-258-7222",
            
       },
       {
             "id": 1,
             "name": "DeRango's Pizza Palace | Italian Restaurant | Racine, WI",
             "domain": "derangos.com",
             "email": "info@derangospizzapalace.com",
             "phone": "262-639-4112",
             
       },
       {
             "id": 2,
             "name": "Michigan's Premier Flooring Installation Services | D.E. McNabb",
             "domain": "demcnabb.com",
             "email": "no email",
             "phone": "(248) 587-1500",
             
       },
}]

我想将它存储在我的 mongo 数据库中,但我不知道如何制作架构,我的实际架构看起来像这样

const mongoose = require("mongoose");

const infoSchema = new mongoose.Schema(
    {
        info: {
            type: String,
            trim: true,
            required: true,
            maxlength: 3200
        }
    },
    { timestamps: true }
);

module.exports = mongoose.model("ScrapedInfo", infoSchema);

这是保存数据的控制器

router.post('/stored', (req, res) => {
        const info = new ScrapedInfo(req.body)
        info.save((err) => {
                if (err) {
                    console.log(err+"error")
                }
                else{
                        console.log('saved')
                }
            });   
});

不知道如果我在控制器中犯了错误,对我来说似乎很好,但是每次我运行控制器的按钮时,我都会遇到 rror ValidationError: info Path info is required

【问题讨论】:

  • 你能不能console.log(req.body),输出是什么。
  • 它是一个包含我首先展示的对象数组的对象

标签: node.js arrays json mongodb object


【解决方案1】:

当尝试使用 mongoose 更新对象数组时,您必须使用 markModified(path) 方法告诉 MongoDB 有待处理的更改。在此之后,您将不得不调用 save() 方法。

示例 我们将模式的 passwordsArr 部分作为对象数组。

//密码Arr已修改

// 将路径标记为有更改以写入数据库 user.markModified('passwordsArr');

// 现在保存文档 user.save(function (err, user) { if (err) return next(err);

【讨论】:

    【解决方案2】:

    您的架构应该如下所示。 “Path info is required error”是由于Schema中不存在键“info”引起的。

        const mongoose = require('mongoose')
        const Schema = mongoose.Schema
        
        // Schema
        const infoSchema = new Schema(
            {
                name: {
                    type: String,
                    required: true,
                },
                domain: {
                    type: String,
                    required: true
                },
                email: {
                    type: String,
                    required: true
                },
                phone: {
                    type: String,
                    required: true
                }
        },
        { timestamps: true }
    )
    
    // Model
    module.exports = mongoose.model('Info',infoSchema)
    

    如果你想保存一个数组,你的“info”键应该是这样的:

    info: {
                type: Array,
                trim: true,
                required: true,
                maxlength: 3200
            }
    

    其中“类型”是数组而不是字符串。

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 2018-07-12
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 2019-07-14
      • 2014-02-10
      相关资源
      最近更新 更多