【问题标题】:How to add name dynamically when pushing data to firebase database?将数据推送到firebase数据库时如何动态添加名称?
【发布时间】:2019-04-15 02:46:26
【问题描述】:

我想将数据推送到 firebase 数据库,我想将动态名称和动态值推送到数据库。我正在使用 JavaScript。

function dbsave(Url){ 
var time = new Date().getTime();
firebase.database().ref('Usersfiles/' +window.logedinuser.uid ).set({
  time : Url /*here it is taking time as a string but i want it to take it as var time which is a var contains value */
  }, function(error) {
    if (error) {
      console.log("failed "+error);
      failedlol();

    } else {
      console.log("success");
    }
  });
}

我想动态推送名称和它的值。 就像用户每次提交数据一样,时间(即名称)也应该改变和值。

【问题讨论】:

  • 到目前为止的代码是否在更新时间?
  • 时间不是取自 var time = new Date().getTime();它需要时间有一个字符串(只有“时间”这个词)。 @KamanaKisinga

标签: javascript firebase firebase-realtime-database


【解决方案1】:

只需将代码 sn-p 更改如下

function dbsave(Url) {
  var time = new Date().getTime();
  firebase.database().ref('Usersfiles/' + window.logedinuser.uid).set({
    time: time /*here it is taking time as a string but i want it to take it as var time which is a var contains value */
  }, function(error) {
    if (error) {
      console.log("failed " + error);
      failedlol();

    } else {
      console.log("success");
    }
  });
}

它不起作用的原因是时间仅用作属性名称 要让它接受变量“时间”,请将此值分配给属性

【讨论】:

  • 是的,但我希望该属性应该是动态的。所以,我用 [time] 它认为它是可验证的。顺便说一句谢谢
【解决方案2】:
function dbsave(Url) {
  var time = new Date().getTime();
  firebase.database().ref('Usersfiles/' + window.logedinuser.uid).set({
 /*add [] to make it as verable*/   [time]: time /*here it is taking time as a string but i want it to take it as var time which is a var contains value */
  }, function(error) {
    if (error) {
      console.log("failed " + error);
      failedlol();

    } else {
      console.log("success");
    }
  });
}

我将“时间:时间”改为“[时间]:时间”

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2018-02-27
    • 2018-04-08
    • 2020-03-25
    • 1970-01-01
    • 2016-11-15
    • 2021-10-07
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多