The problem is I render a view and send some data
console.log(products); // shows an array
res.render('seller/sell',{'shop_id':req.user.shop_id ,'products':products});
and I save the data like this in jade
input(id='shop_id',type='hidden',name='shop_id',value='#{shop_id}')
input(id='pd',type='hidden',name='pd',value='#{products}')
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
and then I try to get the products
var d = $('#pd').val();
console.log(typeof d); //shows string
I know that products shuld be a array otherwise
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
Wont work, but why did I get a string when I need the array?
What did I do wrong?
The problem is I render a view and send some data
console.log(products); // shows an array
res.render('seller/sell',{'shop_id':req.user.shop_id ,'products':products});
and I save the data like this in jade
input(id='shop_id',type='hidden',name='shop_id',value='#{shop_id}')
input(id='pd',type='hidden',name='pd',value='#{products}')
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
and then I try to get the products
var d = $('#pd').val();
console.log(typeof d); //shows string
I know that products shuld be a array otherwise
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
Wont work, but why did I get a string when I need the array?
What did I do wrong?
Share Improve this question edited Dec 25, 2015 at 11:01 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 27, 2012 at 9:25 paynestrikepaynestrike 4,66814 gold badges48 silver badges71 bronze badges 5- Where are you trying to 'get' the products. Jade is just a templating language that 'piles' into HTML.... – Alex Commented Nov 27, 2012 at 10:22
-
how does your string look like? you could do something like
var array = string.split(',');
– zemirco Commented Nov 27, 2012 at 10:25 - what does your array 'products' look like? – Alex Commented Nov 27, 2012 at 10:25
- it looks like this {something},{something},{something} – paynestrike Commented Nov 28, 2012 at 0:27
- you can not do var array = string.split(','); because the something in {something},{something},{something} also has ',' – paynestrike Commented Nov 28, 2012 at 0:28
2 Answers
Reset to default 5You cannot 'store' an array in a hidden input field, but what you could do is store a list of the product id's, something like this:
var productIds = products.map(function(product){return product.id}).toString();
res.render('seller/sell',
{'shop_id':req.user.shop_id ,'products':products, productIds: productIds});
Then, in your jade view:
input(id='shop_id',type='hidden',name='shop_id',value='#{shop_id}')
input(id='pd',type='hidden',name='pd',value='#{productIds}')
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
The value of pd
will now be a ma separated list of product Ids
Not particularly elegant, but it solves the problem.
Best way to show ma separated string Example data = abc,pqr,lmn;
below code in js file
test = data.toString().split('",");
res.render("show.jade",{list: test});
display the code in jade as
table
tbody
each item in list
tr
td #{item}