【问题标题】:How do I push a value only once into firebase database (firebase-admin)?如何将一个值仅推送一次到 firebase 数据库(firebase-admin)?
【发布时间】:2020-06-12 18:25:35
【问题描述】:

我想将消息 id 推送到 ip "list" 中,而不是将相同的 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);

在第一次运行时,它给出了这个输出,它甚至没有进入新 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);

输出:

|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 而不是一次,然后, 它写道:

输出:

|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

【问题讨论】:

    标签: javascript node.js firebase firebase-realtime-database


    【解决方案1】:

    线

    if (Object.values(msgD).map((x, index) => Object.keys(x)[index]).includes(msgD)) 
    

    不检查msgId 的存在。

    Object.value(msgD) 应该返回一个包含当前存在的所有值的数组,例如,[54,55],然后.includes(msgId) 可以检查它是否存在于该数组中。

    【讨论】:

    • 哎呀,我错过了一个大细节。 Object.values(msgD).includes(msgId) 有效吗?
    • 之前应该是!,因为它需要检查它是否不存在然后推送新的ID,但问题是我在上面的@987654328上尝试了很多东西@ 但似乎没有任何工作它总是转到新的 IP 部分我现在需要更好地检查 ref 是否存在......
    • ref.once('value').then( function(snapshot) {if (msgD == ip ) {if (!Object.values(msgD).map((x, index) => Object.keys(x)[index]).includes(msgId)) { 注册所有 ID 输出为 5 New IP 而相同的 once...valuemsgD = snapshot.key; if (snapshot.key == ip && msgD !== null) { @ 987654337@用New ID的输出注册所有ID
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2020-01-12
    • 2018-04-08
    • 2020-03-25
    • 2021-08-28
    • 2021-11-12
    相关资源
    最近更新 更多