0

I am using Jade and

- if (userId !== null)
  != "<script type='text/javascript'>"
  != "userDetail.userId = "+userId.toString()+";"
  - if (friends && friends.length > 0)
    != "userDetail.friends = "+friends+";"
  != "</script>"

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}

1 Answer 1

4

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"}]'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks much. Your example is also perfect for the explanation.

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.