我正在制作一个“定时”的 Firebase 函数,从外部 API 获取数据并将其保存到 Firestore。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { default: Axios } = require("axios");
admin.initializeApp();
exports.getIndexData = functions.pubsub.schedule('every 5 minutes').onRun(async() => {
try {
const response = await Axios(
"https://financialmodelingprep.com/api/v3/quotes/index?apikey=myAPIKEY"
);
const data = response.data;
const writeResult = await admin
.firestore()
.collection("index")
.doc("sorted-index")
.set({ indexData: data,timeStamp:Date.now()});
} catch (error) {
console.log(error);
}
return null;
});
这是我的Firebase云函数代码。当我单独运行该函数时,它完全正常工作,而且我还使用“Google Cloud平台云函数测试”测试了该函数。当我单独运行该函数时,数据成功在Firestore中设置。
但是,当我部署该函数时,它不起作用,我认为这与计划函数有关。
{
"insertId": "184t0npf9hhej7",
"jsonPayload": {
"pubsubTopic": "projects/my-project/topics/firebase-schedule-getIndexData-us-central1",
"targetType": "PUB_SUB",
"status": "UNAUTHENTICATED",
"jobName": "projects/my-project/locations/us-central1/jobs/firebase-schedule-getIndexData-us-central1",
"@type": "type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished"
},
"resource": {
"type": "cloud_scheduler_job",
"labels": {
"job_id": "firebase-schedule-getIndexData-us-central1",
"project_id": "my-project",
"location": "us-central1"
}
},
"timestamp": "2020-12-09T08:48:01.142830977Z",
"severity": "ERROR",
"logName": "projects/my-project/logs/cloudscheduler.googleapis.com%2Fexecutions",
"receiveTimestamp": "2020-12-09T08:48:01.142830977Z"
}
我一直在寻找这个“未经身份验证”的错误,并且有人说我必须更改某些权限选项。因此,我给了所有用户和所有经过身份验证的用户一个云函数调用者权限,但仍然无法工作。
对此有什么想法或解决方案吗?谢谢。
return null;
。具体来说,您应该将return null;
移动到try()
中的const writeResult = await admin...
之后,并且还应在catch()
中的console.log()
之后添加它。 - Renaud Tarnec