【问题标题】:How can I pass a variable to a URL?如何将变量传递给 URL?
【发布时间】:2018-05-17 08:04:59
【问题描述】:

我正在尝试发送一封电子邮件,正文中的链接包含从 Firebase 检索到的值。我已成功检索该值,但我不知道如何将其附加到已列出的链接中。 代码如下:

sendinvite() {
      var user = firebase.auth().currentUser;
      var uid = user.uid;

      firebase.database().ref('/userlist/' + uid + '/' + 'hashKey').once('value').then(function(snapshot) {
        var hashKey = (snapshot.val());
        console.log(hashKey)
      });

      var bcc = [];
      for(var e in this.emailinputs){
        if (this.emailinputs[e].email==null || this.emailinputs[e].email=="" || !this.emailinputs[e].email.includes("@") || !this.emailinputs[e].email.includes("."))
        {
          let alert = this.alerCtrl.create({
            title: 'Error!',
            message: 'There was an error with an email address you entered.',
            buttons: ['Ok']
          });
          alert.present()
        }else {
          bcc.push(this.emailinputs[e].email);
        }
      }
      if(bcc.length > 0) {
        let email = {
          bcc: bcc,
          subject: 'Nudget Invite',
          body: '<a href="https://nudget-72ee4.firebaseapp.com/?hashkey='+hashKey+'">Join my grocery list!</a>',
          isHtml: true
        };
        this.emailComposer.open(email);
      }
}

我希望将变量 hashKey 附加到正文中列出的 URL,但我不确定如何实现。

编辑 1

更新了正文以将变量附加到字符串。我不确定我可以将 Firebase 中的 hashkey 放在哪里以便正确引用它。

【问题讨论】:

  • 您是否尝试过使用标准查询字符串?例如:https://nudget-72ee4.firebaseapp.com/?key=value
  • @Tigger 我目前有'&lt;a href="https://nudget-72ee4.firebaseapp.com/?hashkey='+hashKey+'"&gt;Join my grocery list!&lt;/a&gt;'。问题是我不知道在哪里可以放置 hashKey 变量以供引用。

标签: javascript typescript firebase


【解决方案1】:

我看到的主要问题是您将“hashkey”变量的范围限定为 firebase...function(snapshot) 块。这应该在'uid'变量附近的顶部定义,以便所有这些代码都可以引用它。

然后在你的快照函数中,删除hashkey前面的'var',所以它只是设置现有的变量。

另外,请务必在发送电子邮件之前检查 'hashkey' 是否有非空值。

HTH, 吉姆

【讨论】:

    【解决方案2】:

    我认为问题出在这里:

    firebase.database().ref('/userlist/' + uid + '/' + 'hashKey').once('value').then(function(snapshot) {
        var hashKey = (snapshot.val());
        console.log(hashKey)
    });
    

    您正在一个匿名函数内创建名为 hashKeyvar,然后尝试在该函数之外访问 hashKey

    请尝试以下方法:

    var user = firebase.auth().currentUser;
    var uid = user.uid;
    var hashKey = null;
    
    firebase.database().ref('/userlist/' + uid + '/' + 'hashKey').once('value').then(function(snapshot) {
        hashKey = (snapshot.val());
        console.log(hashKey)
    });
    
    ... snip ...
    
        body: '<a href="https://nudget-72ee4.firebaseapp.com/?hashkey='+ ((hashKey == null) ? 'no-key' : hashKey) +'">Join my grocery list!</a>',
    
    ... snip ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多