node.js - 如何从 Nodejs 打字稿中的 apiResponse 读取数据?
问题描述
我想从 googleapis 调用中读取数据。我正在使用异步等待。但我不确定如何读取我返回的数据。
async function makeCall(params:String){
const apiResponse = await goopleapi.particular.get(params);
console.log(`not really sure how to read -- ${apiResponse}`;
// problem is the log prints [object Object].
}
如何将日志从打印[object Object]
到响应的实际内容?最终我想阅读返回的 json - 我该怎么做?谢谢。
万一很重要。我使用 Firebase 作为 Typescript 的后端
更新 1
有问题的 api 用于计费。如https://www.googleapis.com/auth/androidpublisher
. 我在打电话.purchases.products.get
。当我尝试使用 解析响应时JSON.parse(apiResponse)
,出现错误:
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at getItem (/srv/lib/index.js:31:69)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:145:8)
文档:https ://developers.google.com/android-publisher/api-ref/purchases/products/get
更新 2
这是我需要解析成 json 的 apiResponse。我尝试使用JSON.parse(apiResponse)
但出现错误:
{ config:
{ url: 'https://www.googleapis.com/androidpublisher/v2/applications/mypath',
method: 'GET',
paramsSerializer: [Function],
headers:
{ 'Accept-Encoding': 'gzip',
'User-Agent': 'google-api-nodejs-client/2.0.0 (gzip)',
Authorization: 'Bearer somecode',
Accept: 'application/json' },
params: {},
validateStatus: [Function],
retry: true,
responseType: 'json' },
data:
{ kind: 'androidpublisher#productPurchase',
purchaseTimeMillis: '111222333',
purchaseState: 0,
consumptionState: 1,
developerPayload: '',
orderId: 'some string',
purchaseType: 0 },
headers:
{ 'alt-svc': 'quic=":111"; ma=33445566; v="a string"',
'cache-control': 'private, max-age=0, must-revalidate, no-transform',
connection: 'close',
'content-encoding': 'gzip',
'content-type': 'application/json; charset=UTF-8',
date: 'Fri, 14 Jun 2019 12:40:12 GMT',
etag: '"some string"',
expires: 'Fri, 14 Jun 2019 12:40:12 GMT',
server: 'GSE',
'transfer-encoding': 'chunked',
vary: 'Origin, X-Origin',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-xss-protection': '1; mode=block' },
status: 200,
statusText: 'OK' }
该错误仍然与“更新1”中的相同
解决方案
该对象已被解析。错误来自将 json 传递给 JSON.parse() ,它需要一个字符串。
[object Object] 在模板文字中从 objects toString() 方法返回,以避免这种情况:console.log('Some string',apiResponse);
如果仍然有问题,请尝试 console.log(JSON.stringify(apiResponse))
推荐阅读
- javascript - if(a) & if(a!=null && a!=undefined) 的区别
- asp.net - 在 asp.net 中打印 DataGridView 的问题
- android - Android Studio 默认 Junit Runner
- c# - 使用变量键将文本框绑定到字典
- swift - SpriteKit 中的 NSLayoutConstraint 替代方案
- ansible - 运行剧本时出错
- php - 在电子邮件中显示 Woocommerce 分类
- ruby - 红宝石数相等?
- angular - 在 FormArray 中无法捕获 ngSubmit
- mysql - DBDParams 密码转义