尝试通过预定的 Firebase 函数完成以下操作:
该功能无法部署。错误日志只说 {"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code.
当我在本地运行模拟器套件时,没有出现错误,但数据库没有更新。
我在附加到“onRun”方法的“=>”上遇到了linting错误。不确定这是 Firebase 函数中 ES6 的代码问题还是 ESLint 问题。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
admin.initializeApp();
const db = admin.firestore();
exports.scheduledFunction = functions.pubsub
.schedule("every 2 minutes")
.timeZone("America/New_York")
.onRun(async (context) => {
try {
const querySnapshot = await db.collection("my_collection").get();
console.log(querySnapshot.docs)
const promises = querySnapshot.docs.map(doc => db.collection("my_collection").doc(doc.id).delete());
await Promise.all(promises);
const options = {
"method": "get",
"url": "www.myUrl.com",
"headers": {
"Cookie": "...",
},
};
const axiosResponse = await axios(options);
const apiResponse = JSON.parse(axiosResponse.data);
const parsedResponse = apiResponse["news_results"];
const docCreationPromises = parsedResponse.map(response => db.collection("my_collection").add(parsedResponse))
await Promise.all(docCreationPromises);
return null;
} catch (error) {
console.log(error);
return null;
}
});
答案 0 :(得分:1)
对于 =>
的 ESLint 问题,请尝试在 ESLint 配置中将 ecmaVersion
设置为 8。
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"google",
],
parserOptions: {
ecmaVersion: 8
}
};
<块引用>
当我在本地运行模拟器套件时,没有出现错误,但数据库没有更新。
您在使用 Firestore 模拟器吗?如果是,那么数据将被添加到那里而不是在生产中,因此您只能在模拟器中看到数据。