javascript - Pub Sub Cloud 功能 - 异步等待
问题描述
尝试通过预定的 Firebase 功能完成以下任务:
- 获取 Firestore 集合
- 删除收藏
- 向外部 API 发出获取请求
- 将获取请求结果写入 Firestore 集合。
该功能无法部署。错误日志只说{"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code.
当我在本地运行模拟器套件时,我没有收到任何错误,但数据库没有更新。
我在附加到“onRun”方法的“=>”上收到一个 linting 错误。不确定这是 Firebase Functions 中 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;
}
});
解决方案
对于 ESLint 问题,请=>
尝试ecmaVersion
在您的 ESLint 配置中将 设置为 8。
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"google",
],
parserOptions: {
ecmaVersion: 8
}
};
当我在本地运行模拟器套件时,我没有收到任何错误,但数据库没有更新。
你在使用 Firestore 模拟器吗?如果是,那么数据将被添加到那里而不是在生产中,因此您只能在模拟器中查看数据。
推荐阅读
- android - 我尝试使用 SmsRetrieverClient 从收到的短信中读取 OTP,但广播接收器未调用
- javascript - 将解析的日期与 momentjs 进行比较
- python - 有什么简单的方法可以用 QPainterPath 或在 PyQt 中使用类似的东西来绘制复杂的东西
- php - 与 php 的 ssh 连接
- autodesk-forge - 具有纹理转换问题的 Forge 模型
- c++ - 将结构上无序集中的选定字段存储到向量
- php - 我们可以使用 PHP codeigniter 作为 Web 开发框架并为混合应用程序发送 API 吗?
- c++ - 如何解决继承类中注册的 Q_ENUM?
- android - 使用 Itemtouchhelper 更改房间数据库的顺序
- c# - 在 c# 中格式化日期时出现问题