javascript - 如何在 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()
解决方案
您必须将具有模式模型中定义的属性的对象传递给创建函数(在您的情况下是具有names
属性的对象)。您正在传递imgNames
数组。像这样更改您的代码:
await ImgArray.create({ names: imgNames });
推荐阅读
- enzyme - 如何通过 Enzyme 中的文本查找元素?
- arrays - Yii2中数组的Gridview列
- javascript - 如何使用 .show() 和 .hide() 方法在 .click() 上显示错误日志?
- webhooks - webhook 没有收到来自 kik 的传入消息
- objective-c - 如何关闭 NSURLSession dataTaskWithRequest
- javascript - 选中时从选择中删除选项
- scala - scala rdd中键的元组值
- swift - 更新 Core Data 中的值会导致 Swift 中的重复条目
- android - Xamarin 应用程序在一段时间后自行关闭
- botframework - 我的团队机器人如何响应在它自己的线程上发表的评论?