Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

passing locals array from Express to Javascript

passing locals array from Express to Javascript

Problem

I am using Jade and

- if (userId !== null)
  != ""

In Javascript, userDetail.js,

var userDetail = {};

userDetail.userId = null;
userDetail.friends = [];

When I run this I get - Uncaught SyntaxError: Unexpected token ILLEGAL

I am able to refer to userDetail.userId in JS, but userDetail.friends appears as null. Any clue what's wrong ?

friends is an array of object {id, name, _id}

Problem courtesy of: user644745

Solution

You need to use JSON.stringify(friends) as opposed to the default friends.toString() that you have.

node
> [{id: 42, name: "ray"}].toString()
'[object Object]'
> JSON.stringify([{id: 42, name: "ray"}])
'[{"id":42,"name":"ray"}]'
Solution courtesy of: Peter Lyons

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

passing locals array from Express to Javascript

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×