【问题标题】:How to change [object object] from firebase to a string? [duplicate]如何将 [object object] 从 firebase 更改为字符串? [复制]
【发布时间】:2020-06-21 07:03:20
【问题描述】:

我从我的代码中得到一个对象

       const username = await firebase
        .database()
        .ref("users/" + authUser + "/name")
        .once("value")
        .then(function(snapshot) {
          return snapshot;
        });

它的值为“testname”。它将所有内容正确记录到控制台。但是,如果我尝试重用我的用户名,例如在

      const url = `https://url.com/users/${username}/${authUser}/posts.json?auth=${token}`;

我进入了我的 console.log

https://url.com/users/[object Object]/...

我对 firebase 和 JS 很陌生,但我认为我必须将我的对象转换为字符串?如何做到这一点?

【问题讨论】:

  • JSON.stringify(object)?
  • 现在通过 var myJSON = JSON.stringify(username);但它返回“testname”。你知道怎么去掉“”吗?
  • 去掉这个“”有什么用?
  • 我正在使用用户名通过 REST Api 写入我的 firebase。结构现在是 /users/"testname"/posts 但应该是 "users/testname/posts
  • yourString.substring(1, yourString.length-1) 或使用 yourString.slice(1, -1);

标签: javascript database string firebase


【解决方案1】:

使用以下内容将您的对象转换为字符串

JSON.stringify(object)

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 2020-03-06
    • 1970-01-01
    • 2016-06-16
    • 2013-12-24
    • 2012-06-06
    • 2017-09-16
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多