简体   繁体   中英

Problems with parsing a JSON array objects into an object

first question i post here. I'm learning js and trying to do some APIs for a college project. I have the body of the API that passes a JSON object like that:

    [
        {
            "id":1,
            "issueDate":"2021/11/29 09:33",
            "state": "DELIVERED",
            "products": [{"SKUId":12,"description":"a product","price":10.99,"qty":30},
                        {"SKUId":180,"description":"another product","price":11.99,"qty":20},...],
            "supplierId" : 1,
            "transportNote":{"deliveryDate":"2021/12/29"},
            "skuItems" : [{"SKUId":12,"rfid":"12345678901234567890123456789016"},{"SKUId":12,"rfid":"12345678901234567890123456789017"},...]
        },
        ...
    ]

I have problems parsing products into an array of product object.


class Product {

    constructor(SKUId,description,price, qty,) {
        this.id = id;
        this.price = price;
        this.SKUId = SKUId;
        this.qty = qty;
        this.description = description;

    };

}

module.exports = Product;

And in the end this is the code i use for the parsing:

try {
        if (Object.keys(req.body).length === 0) {
            return res.status(422).json({ error: `Empty body request` }).end();
        }
        
         if (!validate_date(req.body.issueDate) ){
           return res.status(422).json({ error:`Issue Date Not Valid ` }).end();
         }

        if (req.body.products === undefined)
            return res.status(422).json({ error: `Products not defined in the call` }).end();

        if (req.body.supplierId === undefined)
            return res.status(422).json({ error: `SupplierId Not defined` }).end();


        let lastID = await db.getLastIdFromTable('RestockOrder');
        let trID = incrementID(lastID);


      
        let order = new Restock_order(trID, req.body.issueDate, "ISSUED", req.body.supplierId, undefined);
        await db.createRestockOrder(order);

      //THIS IS THE LINE OF THE CODE THAT DOESNT WORK
        const products = req.body.products.map((x) => new Product(x.SKUId, x.description, x.price, x.qty));
      
    
  
        order.addProducts(products);
        products.forEach(async (x) => await db.addProductToSKURestockTable(x));
        return res.status(200).end();
    }

After the command const products = req.body.products.map((x) => new Product(x.SKUId, x.description, x.price, x.qty)); the code launchs an exception (caught in a try- catch block) and i dont know what is the real problem in the parsing. Thanks to everyone who will help me to fix this

your class product is not valid and constructor should start from id parameter

const products = req.body.products.map((x) => new Product(x.id,x.SKUId,... and so on

class Product {
  id: number;
  price: number;
  qty: number;
  description: string;
  SKUId:number;
  constructor(id:number,SKUId:number,description:string,price:number, qty:number) {
      this.id = id;
      this.price = price;
      this.SKUId = SKUId;
      this.qty = qty;
      this.description = description;
  };
}

当您调用 request.body 时,您已经在数组中,因此无需添加 .products 部分

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM