【发布时间】: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