'Getting [object, Object] when printing JSON object from req.body to the console

I'm trying to add a product's detail into my API's PostgreSQL database using Postman but when I print the values of the specs key to the console, I get [object Object] instead of the whole data, so even in my PostgreSQL jsonb column the data is stored as [object, Object].

const createProduct = async (req, res, next) => {
  const { name, description, quantity, price, img_url, specs } = req.body;
  
  // Results in [object Object]
  console.log(specs);

  try {
    await db.query(insertProduct, [
      name,
      description,
      quantity,
      price,
      img_url,
      specs,
    ]);

    res.status(201).json({ message: "Product Added Successfully" });
  } catch (err) {
    console.error(err);
    next(err);
  }
};

This is the json object that is being added in Postman:

{
    "name": "Samsung GALAXY Note II",
    "description": "Samsung Galaxy Note II sports a slightly bigger, 5.5-inch display, a quad-core processor that gets rid of the slight lag in the original Note, and best of all Android 4.1 Jelly Bean right out of the box.",
    "quantity": 10,
    "price": 249.99,
    "img_url": "www.google.com/sexyjutso",
    "specs": {
        "display": [
            {
                "size": "5.5",
                "resolution": "1280 x 720"
            }
        ],
        "camera": [
            {
                "photo": "8 MP",
                "video": "1080p"
            }
        ],
        "hardware": [
            {
                "ram": "2GB",
                "chipset": "Exynos 4412 Quad"
            }
        ],
        "battery": "3100mAh",
        "colors": [
            "Titanium Gray",
            "Marble White",
            "Amber Brown"
        ]
    }
}

I even tried stringifying the specs but that too returns [object, Object].

Is there any way to fix this?



Solution 1:[1]

I was using express-validator and that was sanitizing the specs by trimming and escaping its values and completely messing up the JSON object.

Removing that part fixed the frustrating problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nima