嵌套 Javascript Promise - 从 firestore 获取数据

2024-04-16

在过去的三天里,我一直被这个错误困扰,我已经尝试了几乎所有的方法,尝试以 1000 种方式构建承诺,但似乎没有任何效果。也许我正在失去“大局”,所以希望新的眼睛能有所帮助。谢谢阅读:

我有一个在 Firebase Cloud Functions 中运行的预定函数。代码试图完成的任务是

  1. 检查文档是否过期并将其更改为“非活动”>>这部分有效
  2. 如果文档设置为非活动状态,我想查看 firestore 数据库中是否有相同“类型”的其他文档。如果没有相同类型的其他文档,那么我想从我的文档“类型”中删除该类型。

在我最近的尝试(复制如下)中,我检查快照中是否有文档(这意味着存在另一个相同类型的文档,因此不必删除该文档)。然后如果 res!== true,我会删除该文档。

问题是,由于某种原因, res 永远不是真的......也许“res”承诺在“快照”承诺之前解决?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.scheduledFunction = functions.pubsub
.schedule('0 23 * * *').timeZone('Europe/Madrid')
.onRun( async (context) => {

    const expiredDocs = await admin.firestore().collection('PROMOTIONS_INFO')
    .where('active','==',true)
    .where('expiration', '<=', new Date())
    .get()
    .then(async (snapshot) => {
        await Promise.all(snapshot.docs.map( async (doc) => {
            doc.ref.update({active: false})
            const type = doc.data().business.type
            const id = doc.data().id

            const exists = await admin.firestore().collection('PROMOTIONS_INFO')
                .where('active','==',true)
                .where('business.type','==', type)
                .where('id', '!=', id)
                .limit(1)
                .get()
                .then((snapshot) => {
                    snapshot.docs.map((doc)=>{return true})
                }).then(async (res) => {
                    res===true ? null : 
                    (await admin.firestore().collection('PROMOTIONS_INFO').doc('types')
                    .update('types', admin.firestore.FieldValue.arrayRemove(type)))
                })
            }))
        });
});

为了达到您想要的结果,您可能需要考虑使用批量写入 https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes并将您的代码分成不同的步骤。

一组可能的步骤是:

  1. 获取所有仍有效的过期文档
  2. 没有过期的文件吗?记录结果并结束函数。
  3. For each expired document:
    • 将其更新为非活动状态
    • 存储其类型以供稍后检查
  4. 对于要检查的每种类型,检查是否存在该类型的活动文档,如果不存在,则存储该类型以便稍后删除。
  5. 没有要删除的类型?记录结果并结束函数。
  6. 删除所有需要删除的类型。
  7. 记录结果并结束函数。

上述步骤中,步骤3可以利用批量写入 https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes步骤 6 可以利用arrayRemove()场变换 https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array可以去除一次多个元素 https://googleapis.dev/nodejs/firestore/latest/FieldValue.html#.arrayRemove以减轻数据库的负担。


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.scheduledFunction = functions.pubsub
.schedule('0 23 * * *').timeZone('Europe/Madrid')
.onRun( async (context) => {
    // get instance of Firestore to use below
    const db = admin.firestore();
    
    // this is reused often, so initialize it once.
    const promotionsInfoColRef = db.collection('PROMOTIONS_INFO');

    // find all documents that are active and have expired.
    const expiredDocsQuerySnapshot = await promotionsInfoColRef
        .where('active','==',true)
        .where('expiration', '<=', new Date())
        .get();

    if (expiredDocsQuerySnapshot.empty) {
        // no expired documents, log the result
        console.log(`No documents have expired recently.`);
        return; // done
    } 
    
    // initialize an object to store all the types to be checked
    // this helps ensure each type is checked only once
    const typesToCheckObj = {};
    
    // initialize a batched write to make changes all at once, rather than call out to Firestore multiple times
    // note: batches are limited to 500 read/write operations in a single batch
    const makeDocsInactiveBatch = db.batch();
    
    // for each snapshot, add their type to typesToCheckObj and update them to inactive
    expiredDocsQuerySnapshot.forEach(doc => {
        const type = doc.get("business.type"); // rather than use data(), parse only the property you need.
        typesToCheckObj[type] = true; // add this type to the ones to check
        makeDocsInactiveBatch.update(doc.ref, { active: false }); // add the "update to inactive" operation to the batch
    });
    
    // update database for all the now inactive documents all at once.
    // we update these documents first, so that the type check are done against actual "active" documents.
    await makeDocsInactiveBatch.commit();
    
    // this is a unique array of the types encountered above
    // this can now be used to check each type ONCE, instead of multiple times
    const typesToCheckArray = Object.keys(typesToCheckObj);
    
    // check each type and return types that have no active promotions
    const typesToRemoveArray = (await Promise.all(
        typesToCheckArray.map((type) => {
            return promotionsInfoColRef
                .where('active','==',true)
                .where('business.type','==', type)
                .limit(1)
                .get()
                .then((querySnapshot) => querySnapshot.empty ? type : null) // if empty, include the type for removal
        })
    ))
    .filter((type) => type !== null); // filter out the null values that represent types that don't need removal
    
    // typesToRemoveArray is now a unique list of strings, containing each type that needs to be removed
    
    if (typesToRemoveArray.length == 0) {
        // no types need removing, log the result
        console.log(`Updated ${expiredDocsQuerySnapshot.size} expired documents to "inactive" and none of the ${typesToCheckArray.length} unique types encountered needed to be removed.`);
        return; // done
    }
    
    // get the types document reference
    const typesDocRef = promotionsInfoColRef.doc('types');

    // use the arrayRemove field transform to remove all the given types at once
    await typesDocRef.update({types: admin.firestore.FieldValue.arrayRemove(...typesToRemoveArray) });

    // log the result
    console.log(`Updated ${expiredDocsQuerySnapshot.size} expired documents to "inactive" and ${typesToRemoveArray.length}/${typesToCheckArray.length} unique types encountered needed to be removed.\n\nThe types removed: ${typesToRemoveArray.sort().join(", ")}`);

Note:错误检查被省略并且应该被实施。

批次限制

如果您希望达到每批次 500 次操作的限制,则可以在批次周围添加包装器,以便它们根据需要自动拆分。这里包含一种可能的包装器:

class MultiBatch {
    constructor(dbRef) {
        this.dbRef = dbRef;
        this.batchOperations = [];
        this.batches = [this.dbRef.batch()];
        this.currentBatch = this.batches[0];
        this.currentBatchOpCount = 0;
        this.committed = false;
    }
    
    /** Used when for basic update operations */
    update(ref, changesObj) {
        if (this.committed) throw new Error('MultiBatch already committed.');
        if (this.currentBatchOpCount + 1 > 500) {
            // operation limit exceeded, start a new batch
            this.currentBatch = this.dbRef.batch();
            this.currentBatchOpCount = 0;
            this.batches.push(this.currentBatch);
        }
        this.currentBatch.update(ref, changesObj);
        this.currentBatchOpCount++;
    }
    
    /** Used when an update contains serverTimestamp, arrayUnion, arrayRemove, increment or decrement (which all need to be counted as 2 operations) */
    transformUpdate(ref, changesObj) {
        if (this.committed) throw new Error('MultiBatch already committed.');
        if (this.currentBatchOpCount + 2 > 500) {
            // operation limit exceeded, start a new batch
            this.currentBatch = this.dbRef.batch();
            this.currentBatchOpCount = 0;
            this.batches.push(this.currentBatch);
        }
        this.currentBatch.update(ref, changesObj);
        this.currentBatchOpCount += 2;
    }
    
    commit() {
        this.committed = true;
        return Promise.all(this.batches.map(batch => batch.commit()));
    }
}

要使用此功能,请交换db.batch()在原始代码中new MultiBatch(db)。如果批量更新(例如someBatch.update(ref, { ... }))包含一个字段变换(例如FieldValue.arrayRemove()),请确保使用someMultiBatch.transformUpdate(ref, { ... })相反,单个更新被正确地算作 2 个操作(读取和写入)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌套 Javascript Promise - 从 firestore 获取数据 的相关文章

随机推荐

  • Vim:如何滚动绑定光标线?

    Vim 的分割窗口视图中的两个窗口可以相互绑定滚动通过设置 set scrollbind对于他们俩来说 完成此操作后 如果我在一个窗口中向下滚动光标 其他窗口也会随之向下滚动 但是 两个窗口中的光标并未相互绑定 我用过 set cursor
  • DdlGenerator 构造函数不需要参数?

    我想对我的数据库操作进行单元测试 我发现这段代码 https gist github com nboire 2819920 但是 我收到以下错误 CityGame test info Compiling 2 Java sources to
  • 当设备令牌过期时,是否会自动从 FCM 设备组中删除? [复制]

    这个问题在这里已经有答案了 我对 Firebase Cloud Messaging 还很陌生 而且我还没有能够得到这个问题的满意答案 另外 抱歉 所有的全部停止 不幸的是我在 2017 年买了一台 MacBook 苹果决定用它的键盘进行实验
  • 如何将 dict 转换为 unicode JSON 字符串?

    使用标准库对我来说似乎不可能json模块 使用时json dumps它会自动转义所有非 ASCII 字符 然后将字符串编码为 ASCII 我可以指定它不转义非 ASCII 字符 但是当它尝试将输出转换为 ASCII 时它会崩溃 问题是 我不
  • 基于其他阵列形状的零填充阵列

    我有 K 个特征向量 它们都共享维度 n 但具有可变维度 m n x m 他们都生活在一个列表中 to be padded to be padded append np reshape np arange 9 3 3 array 0 1 2
  • 获取带有 mysqli 结果的行数组

    我需要从结果对象中获取所有行 我正在尝试构建一个包含所有行的新数组 这是我的代码 sql new mysqli config host config user config pass config db name if mysqli con
  • 在哪里可以下载 Windows Phone 开发人员工具?

    我找到了 Web 下载程序 但它们似乎对我不起作用 我如何下载 ISO 格式的最新版本或完整安装程序 我无法使用网络下载器 谢谢 尝试这个 http go microsoft com fwlink LinkId 201927 http go
  • 在 Typescript 对象中添加新属性

    我正在尝试在对象中添加新属性 但打字稿给出错误 错误 TS2339 类型 对象 上不存在属性 数量 product Object qty Number foo this product qty 1 Object是错误的注释 更改您的注释 p
  • 如何更改客户 ID 和订单 ID?

    当前 CustomerID 从 1 开始 而生成的第一个订单已OrderID 100000001 有什么方法可以改变这些字段 所以创建的第一个客户已经 顾客号码900000001并且创建的第一个订单有OrderID 900000001 通过
  • Dagger组件依赖含义

    我正在尝试 Dagger 2 我只是通过测试来了解这个框架 我有一个 ApplicationComponent 需要成为整个应用程序的单例 所以我将其定义如下 Component modules ApplicationModule clas
  • 在 SQL Server 2008 中添加列会锁定表吗?

    我想在大约 1200 万条记录的表上运行以下命令 ALTER TABLE t1 ADD c1 int NULL ALTER TABLE t2 ADD c2 bit NOT NULL DEFAULT 0 我已经在暂存中完成了它 并且时机似乎很
  • 如何在 Swift 中将十六进制数转换为 bin?

    我有字符串变量 var str 239A23F 如何将该字符串转换为二进制数 str toInt 不起作用 您可以使用NSScanner 来自基金会框架 let scanner NSScanner string str var result
  • 如何使x轴上的字体大小变量变小

    我有这段代码来创建条形图 但我想将 x 轴中的名称更改为物种名称 并且我想让字体变小 以便我可以将其全部放入 我尝试过使用cex 功能在各种组合中 但没有起作用 如果有建议我将不胜感激 count lt matrix c 16 102 11
  • Excel 2010 VBA ActiveChart.SetSourceData 失败

    我有一个 Excel VBA 应用程序 该应用程序在 Excel 2003 中运行良好 但在 Excel 2010 中失败 相关代码为 Public Sub Create Chart Dim c Dim OutputText As Stri
  • JUnit 测试时排除 @Component 类的过滤器?

    是否可以排除 Component带注释的类 我想从 JUnit 测试中排除一个特殊的类 我的项目有一个类xEventHandler注释为 Component我不希望 spring 在 junit 测试时使用这个类 我的应用程序 TestCo
  • 按 2 个键对 FireBase 中的数据进行排序

    我构建了游戏应用程序 并将记录保存在 FireBase 的实时数据库中 数据库看看 Ka8xxTgyFB8yYKH50j score 10 seconds 1325 K222xTgyFBF33FD50j score 10 seconds 4
  • 如何使用更少的包绘制二元正态分布的表面和轮廓

    我将绘制二元正态分布的 3D 曲面及其轮廓 可以是任何二元正态分布 我想用persp and contour在我的画中 我在网上搜索了一下 但发现了很多方法 大多数人都使用过一些软件包 但我想以使用更少的软件包甚至不安装任何软件包的方式来执
  • R:rJava 包安装失败

    使用以下命令安装 rJava 时install packages rJava 命令我收到以下错误 checking Java support in R present interpreter usr bin java archiver us
  • 为什么我在比较 Perl 中输入的行时遇到问题?

    我不知道这个简单的交易可能做错了什么 但它不起作用 print OK y or n n ans lt gt print n if ans eq y print ans 我基本上想知道如何测试用户输入 这点代码对我来说不起作用 我只是想打印
  • 嵌套 Javascript Promise - 从 firestore 获取数据

    在过去的三天里 我一直被这个错误困扰 我已经尝试了几乎所有的方法 尝试以 1000 种方式构建承诺 但似乎没有任何效果 也许我正在失去 大局 所以希望新的眼睛能有所帮助 谢谢阅读 我有一个在 Firebase Cloud Functions