node.js - 我不知道如何解决这个错误:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
问题描述
我使用 nodeJS 并表达。我不知道如何解决这个错误,请帮助:) 在服务器中:
app.get("/home", (req, res) => {
userInfos = bll.retrieveDataUser(); // [userEmail, userPassword, userName, userId]
res.send({
userEmail: userInfos[0],
userPassword: userInfos[1],
username: userInfos[2],
uId: userInfos[3]
});
在客户端:
fetch(`/home`).then((res) => {
res.json().then((data) => {
if (data.error) {
throw error
} else {
console.log(data);
}
})
})
谢谢您的帮助 :)
解决方案
如果你想返回 json 那么你应该使用 express 的res.json
方法。
app.get("/home", (req, res) => {
userInfos = bll.retrieveDataUser(); // [userEmail, userPassword, userName, userId]
res.json({
userEmail: userInfos[0],
userPassword: userInfos[1],
username: userInfos[2],
uId: userInfos[3]
});
)};
并且您的获取请求语法也无效。
fetch(`/home`)
.then(res => res.json())
.then((data) => {
if (data.error) {
throw error
} else {
console.log(data);
}
})
推荐阅读
- java - Java API中的空对象模式?
- sql - 保存 SSRS 报告参数
- javascript - 如何在 12 内放置 12(6 对)图像s 随机
- python - Python Lambda 打包和部署 Bash
- reactjs - 谷歌分析目标与动态漏斗
- python - Python 错误处理未按预期工作
- node.js - 不断收到错误:未定义配置属性“vidly_jwtPrivateKey”
- powershell - 如何在另一个 Powershell 脚本的文本框中显示输出?
- python-3.x - 将值作为整数传递,但它表示为字符串
- minio - OpenFaaS 功能无法访问 Minio