簡體   English   中英

TypeError:無法讀取nodejs中未定義的屬性“ push”

[英]TypeError: Cannot read property 'push' of undefined in nodejs

當我嘗試與節點一起運行時,遇到此“ TypeError:無法讀取未定義的屬性'push'”的問題。

我已經定義了需要壓入它們的標題和索引。 這是代碼:

var fs = require("fs");
var sys = require('sys');

var neo4j = require('node-neo4j');
const q = require('q');
const exec = require('child_process').exec;
db = new neo4j('http://neo4j:datasci@localhost:7474');

var neo4jCreateNode = [];
var len;
var lenSub;
var tableName = ["category","customer","lot","minorproduct","part","product","productpart","supplier","transaction"];
var attributeName = [["categoryid","categoryname","description"],
          ["city","country","customerid","customername","phone","postalcode","streetaddr"],
          ["lotid","partid"],
          ["minorproductid","productid"],
          ["partid","partname","supplierid"],
          ["categoryid","productid","productname"],
          ["lotid","minorproductid","partid","productid"],
          ["city","contactname","country","phone","postalcode","streetaddr","supplierid","suppliername"],
          ["customerid","minorproductid","productid","transactionid"]];
var data = "";
var char;
var headtrigger = 0;
var headallow = 0;
var counter = 0;
var productID;
var productname;
var tmp = "";
var header = [];
var index = [];
var neoQuery =[];
var countI = 0;
var countJ = 1;
var countK = 0;
var countL = 0;
var path = "/root/datasci/webserver/src/neo4j/Neo4jlog.txt";
fs.writeFile(path, "Hello", function(error) {});

這是將元素推送到數組的功能:

function moveToArray() 
{
  fs.appendFile(path, "---------------------------------", function(error) {});
  console.log("---------------------------------");
  var dfd = q.defer();
  while(index.length>0){
    index.pop();
  }
  while(header.length>0){
    header.pop();
  }
  headtrigger = 0;
      headallow = 0;
  fs.appendFile(path, data, function(error) {});
  console.log(data);
  for (var i = 0; i < data.length; i++) 
  {
    char = data[i]; 
    if ((char >= "A" && char <= "Z") || (char >= "a" && char <= "z")) 
    {
      headtrigger = 1;
      headallow = 2;
      tmp += char;
    }
    else if (char == " " && headtrigger == 1)
    {
      //console.log("hello " + i);
      header.push(tmp);
      tmp = "";
      headtrigger = 0;
    }
    else if (char == "+" && headallow == 2)
      break;
    else
      continue;
  }
  headtrigger = 0;
  headallow = 0;
  //var index = new Array(header.length);
  for(var l=0;l<header.length;l++)
    index.push([]);

  for (var j = i; j < data.length; j++) 
  {
    char = data[j];
    if (char != "|" && char != "+" && char != "-" && char != "\n") 
    {
      if(data[j] == " " && data[j+1] == " ")
        continue;
      if(data[j-1] != "|" && data[j+1] != "|")
      {
        headtrigger = 1;
        headallow = 2;
        tmp += char;
      }
    }
    else if (char == "|" && headtrigger == 1)
    {
      index[counter].push(tmp);
      //console.log(tmp);
      tmp = "";
      headtrigger = 0;
      counter++;
      if(counter == header.length)
        counter = 0;
    }
    else if (char == "+" && headallow == 2)
    {
      break;
    }
  }

  //console.log(header);
  fs.appendFile(path, index, function(error) {});
  console.log(index);

  dfd.resolve();  
  return dfd.promise;
}

我確實需要做到這一點,但仍然有這個問題。 請幫忙。

index[counter].push(tmp);

可能是因為index [counter]未定義嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM