我正在尝试从Google Analytics(分析)API中获取一些数据,到目前为止,我的功能运行良好,除了无法管理将结果发送回客户端['res.send() ']
const { google } = require('googleapis')
const functions = require('firebase-functions');
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const key = require('./auth.json')
const jwt = new google.auth.JWT(key.client_email, null, key.private_key,
scopes)
const view_id = '######'
exports = module.exports = functions.https.onCall((req, res) => {
jwt.authorize(req, res, (err, result) => {
google.analytics('v3').data.ga.get(
{
auth: jwt,
ids: 'ga:' + view_id,
'start-date': '30daysAgo',
'end-date': 'today',
metrics: 'ga:pageviews'
},
(err, result) => {
console.log(result)
res.send(result)
}
)
})
})
现在,正确的结果已记录在服务器端,但是我无法将其发送回客户端。可以预期的是,“ res.send()”将数据返回给客户端以在UI中使用。
答案 0 :(得分:1)
您正在混淆HTTP triggers和callable triggers。
您定义的是使用functions.https.onRequest
的可调用触发器,但是您错误地假设它接收到请求和结果参数。可调用对象的文档说,您获得了一个数据和一个上下文对象。您还应该返回一个承诺,该承诺将解决您要发送的数据。
似乎您打算使用{{1}}编写HTTP触发器。接收到一个请求和响应对象,然后使用响应对象将数据发送回客户端。