Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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;
  };
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda