Reading [Object: null prototype] object throws `SyntaxError: Unexpected token u in JSON at position 0` error

Reading [Object: null prototype] object throws `SyntaxError: Unexpected token u in JSON at position 0` error

所以我正在尝试读取一个对象的 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 函数没有 return 值或需要回调。使函数 return 成为适当的值,您将不会收到此错误。