【问题标题】:Firebase: Get the first result and update itFirebase:获取第一个结果并更新它
【发布时间】:2025-12-22 00:50:11
【问题描述】:

我想在 Firebase 中创建一个“AccountQueues”对象,该对象将包含帐户和名称,如果没有帐户,则包含“FREE”一词。

帐户队列 -LEf-OrdKi65WG0NkQSG 帐户ID: “GR215” 姓名: “免费”

当用户注册时,我想获取第一个免费的 AccountID 并使用用户名进行更新。谢谢您的帮助。我正在开发具有此功能的 ionic 3 移动应用程序。

【问题讨论】:

    标签: firebase firebase-authentication ionic3


    【解决方案1】:

    您可以使用查询来检查免费帐户并将其限制在第一个搜索结果中。您想使用 snapshotChanges() 来返回对象的键。

        returnedKey = null;
    
        this.fireDB.list('AccountQueues', ref => ref.orderByChild('Name').equalTo('FREE').limitToFirst(1)).snapshotChanges().take(1).subscribe(res => {
    
        res.map(doc => {
    
        this.returnedKey = doc.key; //use this key to update object
    
            });
    
        });
    

    使用查询返回的键,您可以更新该对象

    this.fireDB.object(`AccountQueues/${this.returnedKey}`).update({Name:'Bob'});
    

    【讨论】: