【问题标题】:Pass Array of string to MongoDb Schema将字符串数组传递给 MongoDb Schema
【发布时间】:2021-08-17 02:08:51
【问题描述】:

我想将我的字符串数组传递给我的架构。正如您在 auth.js image:image 中看到的那样,我将图像数组传递给我的 mongodb 属性图像,并且我在 image 中成功获取了字符串数组,但我尝试了$push, $each 之类的一切,但我不知道如何将图像数组传递给我的 Mongodb 模式。希望您能理解我的问题。

Schema.js

const mongoose=require('mongoose');

const mySchema=mongoose.Schema({
    
    image:{
        type:[String],
        required:true,
        value:[String]
    }
    
})


const MyCattle=mongoose.model('cattle_list',mySchema);

auth.js

const finaldata = new MyCattle({
        image:[image]
    })
    
    module.exports=MyCattle;

【问题讨论】:

标签: mongodb express mongoose


【解决方案1】:

我相信这是 this question 的副本,但我将在这里分享答案:

const mySchema = mongoose.Schema({
    image: {
        type: [{ type: String }]
    }
});

// ...

【讨论】:

  • 我试过你的代码,但它仍然显示错误,即(ValidationError: image.0: Cast to [string] failed for value "[{"image":["sprat (2).jpg "]}]" 在路径 "image.0" )
猜你喜欢
  • 1970-01-01
  • 2016-12-08
  • 2022-11-26
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
相关资源
最近更新 更多