我正在将AXIOS请求传递到后端,换句话说,请求主体在这里传递对象数组newItemsObj我正try 将这些对象数组保存到具有架构NewItemsData的Mongoose数据库中.此数据模型引用另一个数据模型MoneyInData,但在try 保存到NewItemsData模型时,我收到了一个ValidationError,指出需要几个字段,请注意,我可能做错了什么,以及如何在数据库中有效地保存此对象array.这里的代码只是很容易理解的代码片段.谢谢,我需要你的帮助.

//NewItemsData Model

const mongoose = require('mongoose');
const MoneyInData = require('./MoneyInData');

const newItemsSchema = new mongoose.Schema({

    productName:{
        type: String,
        required: true
    },
    sellingPriceOfTotal:{
      type: String,
      required: true
    },
    quantityCount: {
        type: Number
    },
    unitOfQuantity:{
        type: String
    },
    sellingPrice:{
        type: Number,
        required:true
    },
    sales:{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'MoneyInData'
    }
})
module.exports = mongoose.model('NewItemsData', newItemsSchema);

//MoneyInData Model

const mongoose = require ('mongoose')

const moneyInDataSchema = new mongoose.Schema({

    totalAmountIn:{
        type: String,
        required: true
    },
    amountRecieved:{
      type: String,
      required: true
    },
    balanceDue: {
        type: Number
    },
    itemDescription:{
        type: String
    },
    modeOfPayment:{
        type: String,
        required:true
    }

})
module.exports = mongoose.model('MoneyInData', moneyInDataSchema)

//Create Method for Writing to the database

// @desc post newItems 
// @route POST /newItems
// @access Private
const createNewItems = asyncHandler(async (req, res) => {

    const  newItemsObj  = req.body
    console.log(newItemsObj)
    if (!newItemsObj) {
        return res.status(400).json({ message: 'All fields are required' })
    }
    const addNewItems = await NewItemsData.create(newItemsObj);

    if (addNewItems) {//created
        res.status(201).json({ message: 'New Item Recorded' })
        console.log(addNewItems)
        console.log(newItemsObj)
    } else {
        res.status(400).json({ message: 'Invalid Item' })
    }
})


//sample array of Object data contained in newItemsObj variable
{
  newItemApiList: [
    {
      productName: 'molly',
      sellingPriceOfTotal: 17400,
      quantityCount: 3,
      unitOfQuantity: 'grams',
      sellingPrice: '5800'
    },
    {
      productName: 'group',
      sellingPriceOfTotal: 9000,
      quantityCount: 2,
      unitOfQuantity: 'cm',
      sellingPrice: '4500'
    }
  ]
}

//Error message:
ValidationError: NewItemsData validation failed: sellingPrice: Path `sellingPrice` is required., sellingPriceOfTotal: Path `sellingPriceOfTotal` is required., productName: Path `productName` is required.
    at Document.invalidate (C:\Users\USER\Desktop\bussiness-books\server\node_modules\mongoose\lib\document.js:3183:32)
    at C:\Users\USER\Desktop\bussiness-books\server\node_modules\mongoose\lib\document.js:2976:17
    at C:\Users\USER\Desktop\bussiness-books\server\node_modules\mongoose\lib\schematype.js:1368:9
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
GET/newItems
GET/newItems

推荐答案

如果newItemApiList是数组,则try :

const addNewItems = await NewItemsData.create(newItemsObj.newItemApiList);

如果您愿意,也可以使用扩展语法

const addNewItems = await NewItemsData.create([...newItemsObj.newItemApiList]);

Javascript相关问答推荐

为什么JavaScript双边字符串文字插值不是二次的?

如何避免移动设备中出现虚假调整大小事件?

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

Angular:动画不启动

单击ImageListItemBar的IconButton后,在Material—UI对话框中显示图像

配置WebAssembly/Emscripten本地生成问题

为什么!逗号和空格不会作为输出返回,如果它在参数上?

cypress中e2e测试上的Click()事件在Switch Element Plus组件上使用时不起作用

VUE 3捕获错误并呈现另一个组件

使用Java脚本导入gltf场景并创建边界框

JS:XML insertBefore插入元素

在HTML语言中调用外部JavaScript文件中的函数

NG/Express API路由处理程序停止工作

AddEventListner,按键事件不工作

JavaScript:如果字符串不是A或B,则

Phaser3 preFX addGlow不支持zoom

用另一个带有类名的div包装元素

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?

调用特定数组索引时,为什么类型脚本不判断未定义

如何将对象推送到firestore数组?