【问题标题】:Error: Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]" (ionic 2 and firebase) [closed]错误:路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]”(ionic 2 和 firebase)[关闭]
【发布时间】:2017-12-12 19:28:15
【问题描述】:
saveDetails(){
  this.afAuth.authState.take(1).subscribe(auth => {
    this.af.object('request/${auth.uid}').set(this.request)
    .then();
  })

执行此方法后,出现错误:Firebase.child failed: First argument is an invalid path: "request/${auth.uid}"。路径必须是非空字符串,并且不能包含“.”、“#”、“$”、“[”或“]”。尝试删除 $ 但这不起作用。很确定我也没有传入空字符串。

【问题讨论】:

  • 如果要使用字符串插值,则需要使用反引号(`)而不是单引号(')来包裹字符串。
  • 天哪,非常感谢!

标签: javascript angular firebase firebase-realtime-database ionic2


【解决方案1】:

如果要使用字符串插值,则需要使用反引号 (`) 而不是单引号 (') 来包装字符串。见下文。

saveDetails(){
  this.afAuth.authState.take(1).subscribe(auth => {
    this.af.object(`request/${auth.uid}`).set(this.request)
    .then();
  })

容易忘记! :)

【讨论】:

    【解决方案2】:

    在这种情况下使用变量的正确方法是这样

    saveDetails(){
      this.afAuth.authState.take(1).subscribe(auth => {
        this.af.object('request/'+ auth.uid).set(this.request)
        .then();
      })
    

    【讨论】:

    • 这个答案是另一个不错的选择。与其一直使用错误的引号,不如坚持使用 + 连接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多