如何将变量插入到 firebase 快照中?

How to interpolate variable into firebase snapshot?

我正在使用 firebase 函数向用户发送通知。我可以像这样使用字符串插值:

      const original = change.after.val();
      const lover = context.auth.uid;

      const recipient_username = original.username;
      const topic = `${recipient_username}_notifications`;

但现在我需要调用数据库从 user_id 中获取用户名,并使用用户名从 'original' 快照中获取 loves 的值,但这不起作用:

return db.ref('/users/' + lover).once('value', (snapshot) =>  {

          var username = snapshot.val().username;
          var love = original.loves.username // I need this to use the variable username, but it is just saying "username"

          console.log("lover username")
          console.log(username)
          console.log("loves")
          console.log(loves)

          const payload = {
            notification:{
              title: "You've got Love!",
              body: `${username} sent you Love! Refresh your inbox to recieve it, and why not send some back!`
            }
          };

如何将 var love = original.loves.username 更改为:var love = original.loves.${username}

数据库如下所示:

   users/
         username: usernamehere
         love/
               otherusername: 10 // the amount of love they sent.

您已在 original 上调用 .val() 将其转换为 Javascript 对象。

遍历 Javascript 对象中的路径可以使用 .loves 辅助函数或使用字符串查找来完成。尝试以下

var username = snapshot.val().username;
var love = original.loves[username];