简体   繁体   中英

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

So I'm attempting to read the count attribute of an object, which is being returned as:

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 } }

When I attempt to do:

const a = JSON.parse(JSON.stringify(temp));
console.log(a);

I get the above mentioned error message, SyntaxError: Unexpected token u in JSON at position 0 .

How do I resolve this?

Unexpected token u in JSON at position 0 is a pretty good indicator that you tried JSON.parse("undefined") . Which means temp must be undefined.

Most likely, your readField function does not return a value or expects a callback instead. Make the function return a proper value and you won't get this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM