Bob Kuhar
Bob Kuhar

Reputation: 11130

String interpolation "Cannot convert object to primitive value at Array.toString"

I'm completely new to Typescript, but am surprised that console.log can render my Array correctly but String Interpolation is all "TypeError: Cannot convert object to primitive value...at Array.toString (native)...".

Here's the code.

  static async createPartsListItemsInOperations(input: Array<PartsListItemQtyInput>, authToken: string): Promise<any> {
    // console.log renders the array just fine
    console.log(input);
    // String interpolation results in TypeError: Cannot convert object to primitive value... at Array.toString (native)...
    const query = `mutation { createPartsListItemsInOperations(input: ${input}) { ${OperationsProvider.fields} } }`;
    const data = await this.execute(query, authToken);
    return data['createPartsListItemsInOperations'];
  }

at runtime the console looks like...

[ { operationId: '7',
    catalogIdentifier: '5c9abda',
    quantity: 1 },
  { operationId: '5',
    catalogIdentifier: '59f7775',
    quantity: 2 },
  { operationId: '4',
    catalogIdentifier: '5c9abda',
    quantity: 3 },
  { operationId: '5',
    catalogIdentifier: '5c9ab8f',
    quantity: 4 },
  { operationId: '6',
    catalogIdentifier: '5c9ab8f',
    quantity: 5 } ]
TypeError: Cannot convert object to primitive value
    at Array.toString (native)
    at Function.<anonymous> (/Users/rkuhar/work/api-gateway/src/api/operations/provider.ts:184:73)
    at Generator.next (<anonymous>)
    at /Users/rkuhar/work/api-gateway/src/api/operations/provider.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/rkuhar/work/api-gateway/src/api/operations/provider.ts:4:12)
    at Function.createPartsListItemsInOperations (/Users/rkuhar/work/api-gateway/src/api/operations/provider.ts:123:16)
    at createPartsListItemsInOperations (/Users/rkuhar/work/api-gateway/src/api/operations/schema.ts:321:32)
    at field.resolve (/Users/rkuhar/work/api-gateway/node_modules/graphql-extensions/src/index.ts:155:61)
    at resolveFieldValueOrError (/Users/rkuhar/work/api-gateway/node_modules/graphql/execution/execute.js:531:18)

How can I get a string representation of my array input into the graphql query string?

Upvotes: 1

Views: 563

Answers (2)

Vishal
Vishal

Reputation: 175

I think you need to send the "input" param as json body instead of sending it in the query string itself. You can JSON.stringify the input array separate from the query string.

Upvotes: 1

bingles
bingles

Reputation: 12213

I don't think console.log calls toString() on arrays, but it seems that string interpolation does based on the error. As you've seen, you can pass an array to console.log, and it will output in a readable form. If you to call input.toString() you will see something like:

"[object Object],[object Object],[object Object],[object Object],[object Object]"

I suspect this is not a valid value for your graphql query. You may need to convert it to an appropriate value before passing it in to the interpolation.

Upvotes: 1

Related Questions