Wednesday 24 May 2017

pug- do an action based on if an element exits in a variable

I am using Express js to build a web app. I use an API to get some data and then send it to a pug file in the below format.

res.render('native.pug', {product_name: body.products, cart_items:body.cart, item_count:body.count, amount: body.totalAmount, sid: sess.id});

body is a json object.

I get id field(along with other fields) in both body.products and body.cart

body.products consists a list of all the products and their details

body.cart consists of details of all the products the user added to the cart.

Now, in the native.pug file, I wish to display different texts, based on if a particular id from body.products is present or not in body.cart

Here is what I tried

if prod.id in cart_items.id
    button Add to Cart
else
    button abc

I get the following error

Add to Cart 23| else 24| button abc Cannot use 'in' operator to search for '590' in undefined

Where am I going wrong exactly?



via krishna

No comments:

Post a Comment