-1

I am using node.js express 4

at my index.js routes file I have this

router.get('/', function(req, res, next) {
    res.render('index', {items: myItemsdocs});
}};

at my index.hbs view I'm trying to use items as a javascript variable

var jsItems = items;
var name1 = jsItems[Object.keys(jsItems)[0]].itemName;
alert(name1);

but nothing happens .. how can I use items object I have in the client side javascript ?

my template engine is express-handlebars

4
  • var jsItems = {{items}}; Commented Nov 20, 2016 at 23:03
  • @chrisG I tried it , doesn't work too. Commented Nov 20, 2016 at 23:05
  • stackoverflow.com/questions/29174185/… Commented Nov 20, 2016 at 23:08
  • My example will only work for numbers; if you have a string, you need var jsItems = "{{items}}"; If it's an array or anything like that, you need to serialize it, then parse it back into an object. Commented Nov 21, 2016 at 0:31

1 Answer 1

0

From the client side you can load them to an array, and the client side can take the that array like below:

// in server side 
var dizi = [true,result._id]; res.send(dizi);

// and the client side (in js file)
var dizi  = response.data;
var mantiksal = dizi[0];
var _id = dizi[1];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.