I am currently facing an issue where I am trying to retrieve an image URL from my MongoDB database using Mongoose, and then display it with EJS. However, I keep getting undefined as the result.
When I use findById to fetch the data, this is what I receive:
[
{
_id: 5b809b2c74e2f54c20ae30da,
brand: 'honda',
model: 'cbr250r',
price: 20000,
amount: 20,
img: 'https://news.maxabout.com/wp-content/uploads/2016/12/22.png',
backImg: 'https://wallpapercave.com/wp/wp3065342.png',
views: 1,
desc: 'this is a bike description'
}
]
I am attempting to access the "backImg" property using the path: "item[0].backImg". However, when I log it in the console, it returns undefined. I have also tried "item.backImg"
This is the route file containing the function:
const router = require('express').Router();
const bodyParser=require('body-parser')
const urlencodedParser =bodyParser.urlencoded({extended:false})
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));
const itemModel = require('../models/itemsModel')
const userModel = require('../models/userModel')
//items//
router.get('/:_id',(req,res)=>{
console.log(req.params._id)
let q = itemModel.find({_id:req.params._id})
q.exec(function(err,item){
if(err){
console.log(err)
} else {
console.log(item)
console.log(item[0].brand)
console.log(item[0].backImg)
console.log(item.backImg)
res.render('./pages/items' , {item:item,user: req.user,} )
}
})
})
module.exports = router