[英]How do I push a value only once into firebase database (firebase-admin)?
我想將消息 ID 推送到 ip“列表”中,而不是將相同的 ID 推送兩次,我嘗試了很多方法但都不起作用如何檢查密鑰是否存在?
let ipsLikes = (ip,msgId) => {
ip = ip.replace(/\./g, "dot");
let msgD;
let ref = db.ref(`ipslikes/${ip}`);
ref.on('value', function(snapshot) {
msgD = snapshot.val();
if (msgD !== null) {
if (Object.values(msgD).map((x, index) => Object.keys(x)[index]).includes(msgD)) {//Object.valuse(msgD).indexOf(msgId) > -1
console.log("New ID");
ref.push(msgId);
}
} else {
console.log("New IP");
ref.push(msgId);
}
});
}
ipsLikes('88.88.88.88',55);
ipsLikes('88.88.88.88',54);
ipsLikes('88.88.88.88',55);
ipsLikes('11.11.11.11',55);
ipsLikes('11.11.11.11',52);
在第一次運行時,它給這個 output 它甚至沒有 go 到新 ID 行:
New IP
New IP
New IP
New IP
New IP
我如何告訴它檢查鍵是否存在然后檢查值? 在不刪除數據庫的情況下第二次運行時,它不會重寫 ID,這很好但還不夠。
更多嘗試:
let ipsLikes = (ip,msgId) => {
ip = ip.replace(/\./g, "dot");
let idsArr = [];
let ref = db.ref(`ipslikes/${ip}`);
ref.on("value", function(snapshot) {
if (snapshot.exists()) {
snapshot.forEach(function(child) {
idsArr.push(child.val());
});
console.log(idsArr+"|"+msgId);
if (!idsArr.includes(msgId)) {
console.log("New ID");
ref.push(msgId);
}
} else {
console.log("Midlle");
ref.push(msgId);
}
});
}
ipsLikes('88.88.88.88',55);
ipsLikes('88.88.88.88',54);
ipsLikes('88.88.88.88',55);
ipsLikes('11.11.11.11',55);
ipsLikes('11.11.11.11',52);
回報:
Midlle
Midlle
Midlle
55|55
55|54
New ID
55|55
55,55,54|55
55,55,54|54
55,55,54|55
55,55,54,55,54,55|55
55,55,54,55,54,55|54
55,55,54,55,54,55|55
55,55,54,55,54,55,55,54,55,54|55
55,55,54,55,54,55,55,54,55,54|54
55,55,54,55,54,55,55,54,55,54|55
Midlle
Midlle
55|55
55|52
New ID
55,55,52|55
55,55,52|52
55,55,52,55,52,52|55
55,55,52,55,52,52|52
並寫道:
let ipsLikes = (ip,msgId) => {
ip = ip.replace(/\./g, "dot");
let idsArr = [];
let ref = db.ref(`ipslikes/${ip}`);
ref.once("value").then( function(snapshot) {
snapshot.forEach(function (child) {
idsArr.push(child.val());
});
console.log(idsArr + "|" + msgId);
if (!idsArr.includes(msgId)) {
console.log("New ID");
ref.push(msgId);
}
});
}
ipsLikes('88.88.88.88',55);
ipsLikes('88.88.88.88',54);
ipsLikes('88.88.88.88',55);
ipsLikes('11.11.11.11',55);
ipsLikes('11.11.11.11',52);
Output:
|55
New ID
|54
New ID
|55
New ID
|52
New ID
|55
New ID
像第一張圖片一樣寫入數據
在不刪除數據庫的情況下第二次運行:
它輸出:
55,54,55|55
55,54,55|54
55,54,55|55
52,55|52
52,55|55
不寫入新數據...
我嘗試了同樣的方法,但使用 on 而不是 once,然后,它寫道:
使用 output:
|55
New ID
|54
New ID
|55
New ID
55|55
55|54
New ID
55|55
55,55,54|55
55,55,54|54
55,55,54|55
55,55,54,55,54,55|55
55,55,54,55,54,55|54
55,55,54,55,54,55|55
55,55,54,55,54,55,55,54,55,54|55
55,55,54,55,54,55,55,54,55,54|54
55,55,54,55,54,55,55,54,55,54|55
|55
New ID
|52
New ID
55|55
55|52
New ID
55,55,52|55
55,55,52|52
55,55,52,55,52,52|55
55,55,52,55,52,52|52
線
if (Object.values(msgD).map((x, index) => Object.keys(x)[index]).includes(msgD))
不檢查是否存在msgId
。
Object.value(msgD)
應該返回當前存在的所有值的數組,例如[54,55]
然后.includes(msgId)
可以檢查它是否存在於該數組中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.