firebase未保存在同一数组中

wrrgggsh  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(152)

firebase不保存在同一数组中我想保存在同一数组中不生成新名称
我的代码:

dbmsg.ref(`Usersmsg/${message.author.id}`).once("value").then(async function(db) {
  if (db.val() == null) {
    dbmsg.ref(`Usersmsg/${message.author.id}`).set({
      user: ["jhon"] //mensagem: [`${message.content} | ${message.guild.name} | ${message.channel.name} | ${ping.getUTCHours()}:${ping.getUTCMinutes()}:${ping.getUTCSeconds()}  ${ping.getUTCDate()}/${ping.getUTCMonth() + 1}/${ping.getFullYear()} | ${message.guild.vanityURLCode}`], 
    }); // Cano o autor não esteja no sistema de xp, ele vai setar um parametro no banco de dados com o xp e level
  } else {
    dbmsg.ref(`Usersmsg/${message.author.id}/mensagem`).push({
      user: ["lety"]
    });

  };
});

输出为:
23011393: mensagem: user: 0 :"jhon" -NGWqzRmWl8H4C6FEJ5L: user: 0: "lety"
我要保存在同一数组中的“user”不生成新名称

vwoqyblh

vwoqyblh1#

push()会在指定的路径中添加一个新节点。您需要在数组中指定index来添加一个项,如下所示:

dbmsg.ref(`Usersmsg/${message.author.id}`).once("value").then(async function(snap) {
  if (snap.val() == null) {
    await dbmsg.ref(`Usersmsg/${message.author.id}`).set({
      user: ["jhon"]
    });
  } else {
    await dbmsg.ref(`Usersmsg/${message.author.id}/user`).update({
      1: "lety"
    });
  };
});

您可以按如下方式获取该阵列中的当前用户数:

const count = Object.keys(snap.val()).length

相关问题