pass data from mongoose to ejs

Viewed 36

I have the following model

const userSchema = new mongoose.Schema({
    username: String,
    likes: Number
})

and a route

app.get("/users/:id", async(req, res) => {
    const id = req.params.id
    let cc = await User.find({ username: id });
    let raw = cc;
    res.render('index', {data : raw})

})

when I hit this route it works fine and on the front end, it prints the array that is returned by cc. i.e

[{"_id":"6282d457f697743c1145a02","username":"John","likes":0,"__v":0}]

But, on the HTML side, if I try to access <%= data[0].likes %> I get an undefined error for "likes"

Similarly, when I try to pass a specific property of the object to ejs for example

res.render('index', {data : raw[0].likes}

I get an error that says Cannot read properties of undefined (reading 'likes')

why is this error occurring

0 Answers
Related