简体   繁体   English

缺少属性ID,Firebase之后的冒号

[英]Missing Colon After Property ID, Firebase

I am writing a program that needs to read and write to Firebase, my server backend. 我正在编写一个需要读写服务器后端Firebase的程序。 I'm not sure why, but the JS interpreter says that I'm missing colons and my definitions are off. 我不确定为什么,但是JS解释器说我缺少冒号,并且我的定义不正确。 Here is the exact error: 这是确切的错误:

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

However, I've clearly defined these things, and my functions are formatted correctly. 但是,我已经清楚地定义了这些内容,并且我的函数格式正确。 Maybe I'm missing something. 也许我想念一些东西。

HTML: 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: 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: 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: app.js:

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

This is the syntax error: 这是语法错误:

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

In modern (ES2015) JavaScript, that would be 在现代(ES2015)JavaScript中,

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

In older versions of JavaScript, there's no compact way of doing it. 在旧版本的JavaScript中,没有紧凑的方法可以做到这一点。 You could write a helper function: 您可以编写一个辅助函数:

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

Then: 然后:

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

The helper function "obj" just treats its arguments as a sequence of name-value pairs of values. 辅助函数“ obj”仅将其自变量视为一系列名称-值对值。 The first argument is the first property name, the second is the first property value, and so on. 第一个参数是第一个属性名称,第二个是第一个属性值,依此类推。 Prior to ES2015, the language did not support the concept of evaluating an expression on the left side of a : in an object initializer in order to determine a property name. 在ES2015之前,该语言不支持在对象初始化器中评估: 左侧的表达式以确定属性名称的概念。 Only property values could be expressions evaluated as part of the interpretation of the object initializer; 只有属性值才能作为表达式进行评估,作为对象初始化程序解释的一部分; property names had to be constants. 属性名称必须是常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM