gpt4 book ai didi

node.js - 将本地数组从 Express 传递到 Javascript

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:21 25 4
gpt4 key购买 nike

我正在使用 Jade

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

在 Javascript 中,userDetail.js,

var userDetail = {};

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

当我运行这个时,我得到 - Uncaught SyntaxError: Unexpected token ILLEGAL

我可以在JS中引用userDetail.userId,但是userDetail.friends显示为null。知道出了什么问题吗?

friends 是一个对象数组 {id, name, _id}

最佳答案

您需要使用 JSON.stringify(friends),而不是默认的 friends.toString()

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

关于node.js - 将本地数组从 Express 传递到 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com