首页 > 解决方案 > 读取 [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.

我该如何解决这个问题?

标签: javascriptprisma

解决方案


Unexpected token u in JSON at position 0是您尝试过的一个很好的指标JSON.parse("undefined")。这意味着temp必须是未定义的。

最有可能的是,您的readField函数不返回值或期望回调。使函数返回正确的值,您将不会收到此错误。


推荐阅读