I add object with categories in a DB. It shows the price and date of addition. How can I print the date to the console? Now, I have in the console: [ [ { cost: ’50$’, date: [Object] } ] ]. And I need to take what is in date. How can i do this?
// my obj
сategories: {
products: {
cost: "50$",
date: {
day: `11`,
month: `11`,
year: `11`,
},
},
},
// get date
mongoClient.connect(function(err, client) {
const db = client.db("expensesdb");
const collection = db.collection("users");
if (err) return console.log(err);
collection.find({
name: "Tom"
}).toArray(function(err, results) {
let res = results.map((user) => user.сategories.map((cat) => cat.products));
console.log(res);
client.close();
});
});
2
Answers
For printing all json data use:
It serializes your
res
variable with 2 indent spaces, so it basically turn your JavaScript object into a Json string equivalent.null
refer to a replacer function.See details on documentation.