首页 > 解决方案 > 如何在 Node Mongodb 中保存字符串数组

问题描述

我有一个字符串数组,我想在 Node/express 中保存在 Mongodb 中,它正在将 Empty 数组保存在 Mongodb 中

const multiImagesSchema = mongoose.Schema({
    names:Array
})

import ImgArray from '../../Model/multiImages.js'

let imgNames = req.files.map(imgName=>{ 
            return imgName.originalname  
        })

      // console,log(imgNames)
       //result in console:   ['Apple,png', 'Grapes.png','Ornage.png','Banana.png'] 

        const imageArray = new ImgArray(imgNames)
        await imageArray.save()

标签: javascriptnode.jsmongodbexpressmongoose

解决方案


您必须将具有模式模型中定义的属性的对象传递给创建函数(在您的情况下是具有names属性的对象)。您正在传递imgNames数组。像这样更改您的代码:

await ImgArray.create({ names: imgNames });

推荐阅读