javascript - 读取 [Object: null prototype] 对象会抛出`SyntaxError: Unexpected token u in JSON at position 0` 错误
问题描述
所以我试图读取count
一个对象的属性,它被返回为:
const PAGINATION_QUERY = gql`
query PAGINATION_QUERY {
itemsConnection {
aggregate {
count
}
}
}
`;
const temp = readField('itemsConnection');
temp = [Object: null prototype] {
__typename: 'ItemConnection',
aggregate:
[Object: null prototype] { __typename: 'AggregateItem', count: 3 } }
当我尝试这样做时:
const a = JSON.parse(JSON.stringify(temp));
console.log(a);
我收到上述错误消息,SyntaxError: Unexpected token u in JSON at position 0
.
我该如何解决这个问题?
解决方案
Unexpected token u in JSON at position 0
是您尝试过的一个很好的指标JSON.parse("undefined")
。这意味着temp
必须是未定义的。
最有可能的是,您的readField
函数不返回值或期望回调。使函数返回正确的值,您将不会收到此错误。
推荐阅读
- python - Tkinter 回调中的异常:未定义名称“selected_book”
- python - flask_sqlalchemy 导入错误:文件“
",第 1 行,在 无法从“app”导入名称“db” - postgresql - Docker-Compose postgres 升级initdb:错误:目录“/var/lib/postgresql/data”存在但不为空
- javascript - 为什么我得到的输出是“\”userName\“”而不是那个“userName”?
- apache-spark - Spark:根据另一列从地图中提取值
- c++ - 获取重定向传入的输入文件名?
- random - LUA 中是否有任何非全局种子随机数?
- powershell - 将 curl 与默认凭据一起使用
- java - 使用 Selenium webdriver 按 Ctrl+Shift+c 不起作用
- java - Graphics2d 绘制带有部分粗体文本的文本