Home > Mobile >  Problems with parsing a JSON array objects into an object
Problems with parsing a JSON array objects into an object

Time:05-13

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

CodePudding user response:

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;
  };
}

CodePudding user response:

When you call to request.body you already are in the array, so no need to add the .products part

  • Related