我一直在成功使用Google Sheets API。我设法使用了spreadsheets.values.update
和spreadsheets.values.get
方法,没有出现错误。今天,我一直在尝试使用方法:spreadsheets.values.append
。此方法只是将一行数据附加到表的末尾。在开发人员门户中,您可以“尝试使用此api”,当我收到200响应时。
但是,当我在NodeJS / Express应用程序中运行完全相同的方法(包括参数!)时,出现错误消息:
“ TypeError:将圆形结构转换为JSON”,
但是,当我查看Google表格时,数据已成功添加。这是我的代码:
function postValid(req, res) {
const data = req.body;
console.log('received validate post');
sheets.spreadsheets.values.append( {
auth: jwtClient,
spreadsheetId: ssId,
range: 'Valid!A:C',
resource: {
values: [
[1, 2, 3]
]
},
valueInputOption: 'USER_ENTERED',
}, (err, response) => {
if (err) {
res.status(500);
res.send('The API returned an error: ' + err);
}
res.send(response)
})
}
随着数据的发送,我可以忽略该错误,但是我真的很想知道是什么导致了这种循环结构错误。我该如何调试呢?
就像我说的那样,具有几乎相同结构的get和update方法可以正常工作,因此它不是{auth: jwtClient, spreadsheetId: ssId, }
部分,其余部分都是非常标准的JSON。我就是不明白。
有人遇到同样的问题吗?
更新: 我已经解决了这个问题。如果禁用Express的JSON主体解析器,则可以查看完整响应。在下面,您会看到循环引用是“请求”的一部分,但是此请求是通过 googleapi 方法完成的,这是否可能是 googleapi 的错误库?
{ status: 200,
statusText: 'OK',
headers:
{ 'content-type': 'application/json; charset=UTF-8',
vary: 'X-Origin, Referer, Origin,Accept-Encoding',
date: 'Tue, 30 Oct 2018 12:22:09 GMT',
server: 'ESF',
'cache-control': 'private',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'quic=":443"; ma=2592000; v="44,43,39,35"',
'accept-ranges': 'none',
connection: 'close' },
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: 2147483648,
validateStatus: [Function],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
Authorization:
<REMOVED>,
'User-Agent': 'google-api-nodejs-client/1.6.1',
'Content-Length': 29 },
method: 'post',
url:
'https://sheets.googleapis.com/v4/spreadsheets/<REMOVED>/values/Valid!A:C:append',
paramsSerializer: [Function],
data: '{"values":[[null,null,null]]}',
params: { valueInputOption: 'USER_ENTERED' } },
request:
ClientRequest {
_events:
{ socket: [Function],
abort: [Function],
aborted: [Function],
error: [Function],
timeout: [Function],
prefinish: [Function: requestOnPrefinish] },
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: 'sheets.googleapis.com',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'sheets.googleapis.com',
_readableState: [ReadableState],
readable: false,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: null,
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular], //<--HERE IS ONE ISSUE
write: [Function: writeAfterFIN],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 78,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 575,
[Symbol(kBytesWritten)]: 517,
[Symbol(connect-options)]: [Object] },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: 'sheets.googleapis.com',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: null,
_parent: null,
_host: 'sheets.googleapis.com',
_readableState: [ReadableState],
readable: false,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: null,
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular], //<--Again another problem
write: [Function: writeAfterFIN],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 78,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 575,
[Symbol(kBytesWritten)]: 517,
[Symbol(connect-options)]: [Object] },
_header:
'POST /v4/spreadsheets/<REMOVED>/values/Valid!A:C:append?valueInputOption=USER_ENTERED HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nAuthorization: <REMOVED> User-Agent: google-api-nodejs-client/1.6.1\r\nContent-Length: 29\r\nHost: sheets.googleapis.com\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent:
Agent {
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'POST',
path:
'/v4/spreadsheets/<REMOVED>/values/Valid!A:C:append?valueInputOption=USER_ENTERED',
_ended: true,
res:
IncomingMessage {
_readableState: [ReadableState],
readable: false,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
socket: [TLSSocket],
connection: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 200,
statusMessage: 'OK',
client: [TLSSocket],
_consuming: true,
_dumped: false,
req: [Circular], //<--Again another circular reference
responseUrl:
'https://sheets.googleapis.com/v4/spreadsheets/<REMOVED>/values/Valid!A:C:append?valueInputOption=USER_ENTERED',
redirects: [] },
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable:
Writable {
_writableState: [WritableState],
writable: true,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 29,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest: [Circular], //<-- Again!!
_currentUrl:
'https://sheets.googleapis.com/v4/spreadsheets/<REMOVED>/values/Valid!A:C:append?valueInputOption=USER_ENTERED' },
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]:
{ accept: [Array],
'content-type': [Array],
authorization: [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array] } },
data:
{ spreadsheetId: <REMOVED>,
tableRange: 'Valid!A1:C8',
updates:
{ spreadsheetId: <REMOVED>,
updatedRange: 'Valid!A9' } } }
答案 0 :(得分:1)
这确实来自Google api响应。调用其他API(例如https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query)时,我也看到了它 您必须确保仅调用响应的相关部分,而不是尝试将其转换为JSON,以避免循环错误消息