简体   繁体   中英

How to include javascript in ejs to check if value exists in array

Inside my products document I have an array of objects with user Id's. When I render a page I want to check if the logged in user's ID is in that array and if it is, I want to show them something different to users who ID is not in the array.

So, my DB query gets the individual product detail based on the ID in the url string and then renders the view. Then in the view I am performing the loop like this:

<% for(const requests of product.requests) { %>
  <% if (requests.userId.includes(userId) ) { %>
    User exists
  <% } %>
<% } %>

But I can't use 'includes' like that inside ejs. How can I achieve this then?

I have included my query here in case I should be doing this with the query instead of trying to do it in the view.

Product.findOne({titleSlug: titleSlug, _id: productId})
.populate('userId', 'firstName username')
.exec()
.then(product => {
// res.render(.......)

this is how the data is presented when logged to console.

 requests:
   [ { _id: 5c3ed473dee9b803b2282cc6,
       userId: 5c375260a6f58308e510711a,
       firstName: 'Joe',
       motivation: 'some text' },
     { _id: 5c3ed5821abce003c27c682c,
       userId: 5c25cddc73d10c02a75b3d55,
       firstName: 'Bob',
       motivation: 'test' } ] }

But in the view I can access just the ID by looping over requests.userId which outputs this in the view:

5c375260a6f58308e510711a
5c25cddc73d10c02a75b3d55

includes only works on a array.. but in your case you do not use includes() for an array that's why it doesn't work. using this

for(const requests of product.requests)

you select this below part of the whole array

       { _id: 5c3ed473dee9b803b2282cc6,
       userId: 5c375260a6f58308e510711a,
       firstName: 'Joe',
       motivation: 'some text' }

after that using this line

if (requests.userId.includes(userId)

you try to use non array object as an array which is incorrect. try viewing requests.userId. that means you are accessing the previously selected object's property of userId. same way use can access all data of that object like this

 <% for(const requests of product.requests) { %>
    requests.motivation
    requests.firstname
    requests._id 
 <% } %>

like wise... So to achieve what you desire, you can take an approach like this.. considering you have the userID that you want to check against the value you seek in that object array, try doing it like this.

<% for(const requests of product.requests) { %>
  <% if (requests.userId.toString()==userId) ) { %>
    //User exists.. now you can do what you want if the user exists
  <% } %>
<% } %>

the problem was with the way you thought about the object array and accessing it. This should solve the problem you are having... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM