将数据推送到Firebase数据库时如何动态添加名称?

时间:2019-04-02 16:37:24

标签: javascript firebase firebase-realtime-database

我想将数据推送到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");
    }
  });
}

我想同时推送名称和值。 就像每次用户提交数据一样,时间(即名称)也应该更改并值。

2 个答案:

答案 0 :(得分:0)

只需如下更改代码段

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");
    }
  });
}

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

答案 1 :(得分:0)

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");
    }
  });
}

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