物业ID,Firebase后缺少​​冒号

时间:2016-01-10 15:27:10

标签: javascript json html5 firebase

我正在编写一个程序,需要读取和写入我的服务器后端Firebase。我不知道为什么,但JS翻译说我缺少冒号而且我的定义都没有了。这是确切的错误:

SyntaxError: missing : after property id firebase.js:14:31
ReferenceError: refs is not defined visview.js:18:1
ReferenceError: addUser is not defined

但是,我已经明确定义了这些内容,并且我的函数格式正确。也许我错过了什么。

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Web of Hunger</title>
  </head>
  <body>
    <div id="visGraph" class="visNetwork">
      <canvas height="100%" width="100%" style="position: relative; -moz-user-select: none; width: 100%; height: 100%;"></canvas>
    </div>

    <link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.12.0/vis.min.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.12.0/vis.min.js"></script>
    <script type="text/javascript" src="https://cdn.firebase.com/js/client/2.3.2/firebase.js"></script>
    <script type="text/javascript" src="./js/firebase.js"></script>
    <script type="text/javascript" src="./js/visview.js"></script>
    <script type="text/javascript" src="./js/app.js"></script>
  </body>
</html>

firebase.js:

var refs = {
      root : new Firebase("Sorry, I am not allowed to show the URL"),
      users : new Firebase("Sorry, I am not allowed to show the URL"),
      graph : {
        graph : new Firebase("Sorry, I am not allowed to show the URL"),
        nodes : new Firebase("Sorry, I am not allowed to show the URL"),
        edges : new Firebase("Sorry, I am not allowed to show the URL")
      }
    };

    //data manipulation functions (low level to high)

    var addNode = function(node){
      refs.graph.nodes.update({node.name : "temp"});
      refs.graph.nodes.child(node.name).set({
        "id" : node.id,
        "label" : node.label
      });
    };

    var addEdge = function(edge) {
      refs.graph.edges.update({edge.name : "temp"});
      refs.graph.edges.child(edge.name).set({
        "from" : edge.from,
        "to" : edge.to,
        "arrows" : edge.arrows
      });
    };

    var addUser = function(name, edges)
    {
      if(refs.users.child(name) === null)
      {
        var time = Firebase.ServerValue.TIMESTAMP;

        if(refs.graph.nodes.child(name) === null && refs.graph.edges.child(name) === null)
        {
          refs.users.update({name : {}});
          refs.users.child(name).update({
            "id" : time,
            "edges" : {}
          });
          refs.users.child(name).child("edges").update({
            "to" : {},
            "from" : {}
          });

          addNode(new Node(name, time, name));

          for(var i = 0; i < edges.length; i++)
          {
            var nme = edges[i].name;
            refs.users.child(name).child("edges/to").update({nme : true});
            addEdge(new Edge(name, time, edges[i].to, edges[i].arrows));
            //TODO add a "from" edge so that you know who wants to eat you
          }
        }
      } else {
        alert("Username, node name, or edge name taken.");
      }
    };

visview.js:

//handy data structures

var Node = function(name, id, label) {
  this.name = name;
  this.id = id;
  this.label = label;
};

var Edge = function(name, frm, to, arrows) {
  this.name = name;
  this.from = frm;
  this.to = to;
  this.arrows = arrows;
};

//functions that allow vis to read firebase data

refs.graph.graph.on("value", function(snapshot) {
  console.log(snapshot.val());

  //main vis update section
  getGraphDataAsVis(snapshot.val());

});

var getGraphDataAsVis = function(data) {
  //var nodes = refs.graph.child("nodes").
};

app.js:

addUser("tester1", [new Edge("test1", 0, 1, 'to')]);
addNode(new Node("tester2", 400, "tester2"));

1 个答案:

答案 0 :(得分:0)

这是语法错误:

  refs.graph.nodes.update({node.name : "temp"});

在现代(ES2015)JavaScript中,那将是

  refs.graph.nodes.update({ [node.name] : "temp"});

在旧版本的JavaScript中,没有紧凑的方法。你可以写一个辅助函数:

  function obj() {
    var rv = {};
    for (var i = 0; i < arguments.length; ++i)
      rv[arguments[i]] = rv[arguments[i+1]];
    return rv;
  }

然后:

  refs.graph.nodes.update(obj(node.name, "temp"));

助手功能&#34; obj&#34;只是将其参数视为一系列名称 - 值对的值。第一个参数是第一个属性名称,第二个是第一个属性值,依此类推。在ES2015之前,该语言不支持在对象初始值设定项中评估: left 侧的表达式以确定属性名称的概念。只有属性值可以作为对象初始值设定项解释的一部分进行计算;属性名称必须是常量。