Javascript firebase数据库中的数据筛选故障
我正在尝试使用云函数从firebase数据库中筛选一些数据。 这些数据如下所示:Javascript firebase数据库中的数据筛选故障,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在尝试使用云函数从firebase数据库中筛选一些数据。 这些数据如下所示: "items": { "id1": { "status": { "creation": timestampValue, "status": "initialized" }, "data" : data1 } "id2": { "status": { "sta
"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
}
}
我想使用基于创建字段的过滤器。
这个领域并不总是存在
我的代码受此启发:
以下是我编写的代码:
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);
});
});
执行此操作时,查询响应中没有结果。更改此操作:
const oldItemsQuery = ref.orderByChild('creation').endAt(cutoff);
为此:
const oldItemsQuery = ref.orderByChild('creation').equalTo(cutoff);
创建包含与指定值匹配的子项的查询
使用startAt()
、endAt()
和equalTo()
可以为查询选择任意的起点和终点
谢谢你的回复。但我不想得到等于截止值的值,我想得到小于或等于截止值的值。try
orderByChild('creation')。startAt(0)。endAt(截止)代码>也可以在执行constOldItemSquery=ref.orderByChild('creation').endAt(截止)时执行代码>您应该获得所有创建值小于或等于截止值的节点。我确实希望通过以下方法获得小于或等于截止值的值:ref.orderByChild('creation')。endAt(截止值);但事实并非如此,我得到的是所有项目,不管它们的创建时间戳。我将尝试使用startAt(0)您是否尝试过此orderByChild('creation')。startAt(0).endAt(截止)代码>thoughI dit try orderByChild('creation').startAt(0).endAt(截止);我没有得到任何结果。
const oldItemsQuery = ref.orderByChild('creation').equalTo(cutoff);