我对Node.js和MongoDB相当陌生。
所以我在Nodejs中建立了一个如下所示的架构(如下所示)-数据架构
// create Data Schema
const dataSchema = new mongoose.Schema({
allItems: {
// store all expenses here
exp: [{
description: { type: String, require: true, min: 2, max: 50 },
value: { type: Number, require: true },
percentage: { type: Number, require: true }
}],
// store all incomes here
inc: [{
description: { type: String, require: true, min: 2, max: 50 },
value: { type: Number, require: true }
}]
},
// store total incomes & expenses
totals: {
// sum of total expenses, init = 0
exp: { type: Number, require: true, default: 0},
// sum of total incomes, init = 0
inc: { type: Number, require: true, default: 0}
},
// store total budgets, init = 0
budget: { type: Number, require: true, default: 0 },
// store percentage of total budget
// init -1 = something is not in existance yet
percentage: { type: Number, require: true, default: -1 }
})
并有一个后路由器将新数据保存到数据库(如下所示)
router.post('/', async (req, res) => {
// hold exp & inc array length
expLength = req.body.allItems.exp.length
incLength = req.body.allItems.inc.length
// do something to hold allItems exp & inc data first
// do some code here?
// create new data
const data = new Data({
allItems: {
exp: [
{
description: req.body.allItems.exp,
value: req.body.allItems.exp,
percentage: req.body.allItems.exp
}
],
inc: [
{
description: req.body.allItems.inc,
value: req.body.allItems.inc
}
]
},
totals: {
exp: req.body.totals.exp,
inc: req.body.totals.inc
},
budget: req.body.budget,
percentage: req.body.percentage
})
try {
const saveData = await data.save()
res.json(saveData)
} catch(err) {
res.json({
message: err
})
}
})
因此,我正在使用Postman帮助我学习有关nodejs和mongodb的知识。
在邮递员中,我已准备好执行此数据(如下所示)。在哪里,我在allItems下的数组exp中有2个对象。
{
"allItems": {
"exp": [
// first object
{
"description": "Rent",
"value": 500,
"percentage": 50
},
// second object
{
"description": "Loan",
"value": 250,
"percentage": 25
}
],
"inc": [
{
"description": "Web",
"value": 1000
}
]
},
"totals": {
"exp": 750,
"inc": 1000
},
"budget": 250,
"percentage": 75
}
但不幸的是,它只能保存1个对象,这是自从我在后路由器中将数组设置为“ 0”以来的第一个对象(如下所示)。
{
"totals": {
"exp": 750,
"inc": 1000
},
"budget": 250,
"percentage": 75,
"_id": "5e6139b3111a7b593ce296cd",
"allItems": {
"exp": [
// only save the first object
{
"_id": "5e6139b3111a7b593ce296ce",
"description": "Rent",
"value": 500,
"percentage": 50
}
],
"inc": [
{
"_id": "5e6139b3111a7b593ce296cf",
"description": "Web",
"value": 1000
}
]
},
"__v": 0
}
我想我应该先做一个for循环或其他事情来存储inc&exp数据数组。但是我只是不知道如何。
我一直在通过Google搜索。但是还没有运气。无论如何,任何帮助将不胜感激。谢谢。
顺便说一句,它给了我这个错误(如下所示)-
"_message": "Data validation failed",
"message": "Data validation failed: allItems.exp.0.description: Cast to String failed for value \"[\n { description: 'Rent', value: 500, percentage: 50 },\n { description: 'Loan', value: 250, percentage: 25 }\n]\" at path \"description\", allItems.exp.0.value: Cast to Number failed for value \"[\n { description: 'Rent', value: 500, percentage: 50 },\n { description: 'Loan', value: 250, percentage: 25 }\n]\" at path \"value\", allItems.exp.0.percentage: Cast to Number failed for value \"[\n { description: 'Rent', value: 500, percentage: 50 },\n { description: 'Loan', value: 250, percentage: 25 }\n]\" at path \"percentage\", allItems.inc.0.description: Cast to String failed for value \"[ { description: 'Web', value: 1000 } ]\" at path \"description\", allItems.inc.0.value: Cast to Number failed for value \"[ { description: 'Web', value: 1000 } ]\" at path \"value\"",
"name": "ValidationError"
最佳答案
您可以在req.body.allItems.exp上放置forEach循环,并准备将expArr数组保存在exp中:
var expArr = [];
req.body.allItems.exp.forEach(function(eachObj){
var obj = {
"description": eachObj.description,
"value": eachObj.value,
"percentage": eachObj.percentage
}
expArr.push(obj);
});
尽管通过查看您的代码似乎不需要任何循环,但是您可以像这样直接保存exp数据:
router.post('/', async (req, res) => {
// hold exp & inc array length
expLength = req.body.allItems.exp.length
incLength = req.body.allItems.inc.length
// do something to hold allItems exp & inc data first
// do some code here?
// create new data
const data = new Data({
allItems: {
exp: req.body.allItems.exp,
inc: req.body.allItems.inc
},
totals: {
exp: req.body.totals.exp,
inc: req.body.totals.inc
},
budget: req.body.budget,
percentage: req.body.percentage
})
try {
const saveData = await data.save()
res.json(saveData)
} catch(err) {
res.json({
message: err
})
}
})