我对Typescript完全陌生,但是console.log
可以正确呈现我的Array却感到惊讶,但String Interpolation都是“ TypeError:无法将对象转换为Array.toString(原始)的原始值。 ..”。
这是代码。
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'];
}
在运行时,控制台看起来像...
[ { 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)
如何获取我在graphql查询字符串中输入的数组的字符串表示形式?
答案 0 :(得分:1)
我不认为console.log
在数组上调用toString(),但是字符串插值似乎是基于错误的。如您所见,您可以将数组传递给input.toString()
,并且它将以可读的形式输出。如果您致电"[object Object],[object Object],[object Object],[object Object],[object Object]"
,您会看到类似以下内容的信息:
frameRate()
我怀疑这不是您的graphql查询的有效值。在将其传递到插值之前,可能需要将其转换为适当的值。
答案 1 :(得分:1)
我认为您需要将“输入”参数作为 json正文发送,而不是通过查询字符串本身发送。您可以JSON.stringify
将输入数组与查询字符串分开。