I’m writing app in node.js. In the Order model I have a “userData” object that has an “email” element. How do I refer to “email” when using the find method?
order model:
const orderSchema = new Schema({
userData: { // <--- email is in the userData
firstname: String,
email: String // <--- I want this
},
items: {}
});
module.exports = mongoose.model('order', orderSchema, 'orders');
use of the order:
router.put('/user-orders', (req, res) => {
const data = req.body;
Order.find({ userData.email: data.email }, (error, order) => { // <--- it doesn't work
if (error) {
console.log(error);
} else {
return res.json({ order });
}
})
})
Advertisement
Answer
router.put('/user-orders', (req, res) => {
const data = req.body;
Order.find({ "userData.email": data.email }, (error, order) => { // <--- it doesn't work
if (error) {
console.log(error);
} else {
return res.json({ order });
}
})
})You are missing some quotes for the keys. It needs to be “userData.email”.