【问题标题】:Trouble filtering data in firebase database过滤firebase数据库中的数据时遇到问题
【发布时间】:2018-12-13 15:27:09
【问题描述】:

我正在尝试在云功能中过滤来自 firebase 数据库的一些数据。 该数据如下所示:

"items": {
    "id1": {
        "status": {
            "creation": timestampValue,
            "status": "initialized"
        },
        "data" : data1
    }
    "id2": {
        "status": {
            "status": "loaded"
        },
        "data" : data2
    },
    "id2": {
        "status": {
            "creation": timestampValue,
            "status": "loaded"
        },
        "data" : data
    },
    "id3": {
        "status": {
            "creation": timestampValue,
            "status": "ended"
        },
        "data" : data3
    }
}

我想使用基于创建字段的过滤器。 该字段并不总是存在。

我的代码受此启发: https://github.com/firebase/functions-samples/blob/master/delete-old-child-nodes/functions/index.js

这是我写的代码:

const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 24 Hours in milliseconds.

exports.cleanAfter24h = functions.database.ref('/items/{itemId}').onUpdate((change, event) => {
    const ref = change.after.ref.parent; // reference to the parent
    const now = Date.now();
    const cutoff = now - CUT_OFF_TIME;
    const oldItemsQuery = ref.orderByChild('creation').endAt(cutoff);
    return oldItemsQuery.once('value').then((snapshot) => {
        // create a map with all children that need to be removed
        const updates = {};
        snapshot.forEach(child => {
            let childData = child.val();
            if (childData.status.creation) {
                let elapsed = childData.status.creation - cutoff;
                if (elapsed <= 0) {
                    updates[child.key] = null;
                }
            } else {
                console.log(child.key + ' does not have a creation date');
            }
        });
        // execute all updates in one go and return the result to end the function
        return ref.update(updates);
  });
});

当代码运行时,所有项目都会被检索,即使是那些时间戳小于截止值的项目和那些没有创建字段的项目。 有什么建议可以解决这个问题吗?

我尝试通过在 endAt 之前添加 startAt("") 来删除没有创建字段的项目,如下所示: Firebase, query with "a child exists" as a condition?

const oldItemsQuery = ref.orderByChild('creation')startAt("").endAt(cutoff);

这样做我在查询响应中没有结果。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    改变这个:

    const oldItemsQuery = ref.orderByChild('creation').endAt(cutoff);
    

    进入这个:

    const oldItemsQuery = ref.orderByChild('creation').equalTo(cutoff);
    

    equalTo

    创建一个包含与指定值匹配的子项的查询。

    使用startAt()endAt()equalTo() 允许我们为查询选择任意起点和终点。

    【讨论】:

    • 感谢您的回复。但我不是想获得等于截止值的值,我想获得低于或等于截止值的值。
    • 尝试orderByChild('creation').startAt(0).endAt(cutoff); 同样,当您执行const oldItemsQuery = ref.orderByChild('creation').endAt(cutoff); 时,您应该获得所有创建小于截止或等于截止的节点..
    • 我确实希望得到低于或等于截止值的值: ref.orderByChild('creation').endAt(cutoff);但事实并非如此,无论它们的创建时间戳如何,我都得到了所有项目。我会尝试 startAt(0)
    • 你试过这个orderByChild('creation').startAt(0).endAt(cutoff);
    • 我尝试 orderByChild('creation').startAt(0).endAt(cutoff);我没有得到任何结果。
    猜你喜欢
    • 2021-04-13
    • 2021-10-12
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多