Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点JS文件读取后为空_Javascript_Node.js - Fatal编程技术网

Javascript 节点JS文件读取后为空

Javascript 节点JS文件读取后为空,javascript,node.js,Javascript,Node.js,我试图在一个单独的JSON文件中记录我看到的每个Twitter ID的tweets位置。为每个tweet调用以下代码,并应为每个新ID创建一个新的JSON文件,并附加当前tweet的位置: console.log("@" + tweet.user.screen_name + " - " + tweet.user.name); timeStampNow = "[" + date.getDate() + ":&q

我试图在一个单独的JSON文件中记录我看到的每个Twitter ID的tweets位置。为每个tweet调用以下代码,并应为每个新ID创建一个新的JSON文件,并附加当前tweet的位置:

console.log("@" + tweet.user.screen_name + " - " + tweet.user.name);
    
    timeStampNow = "[" + date.getDate() + ":" + date.getMonth() + ":" + date.getFullYear() + "-" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "]";
    
    console.log(timeStampNow + " " +  tweet.place.full_name);
    
    fs.exists(userData + "/" + tweet.user.id + ".json", function(exists) {

      //Is executed if file does not Exists
      if (!exists){
        console.log("Person Not Recognised. Adding to Folder");
        json = {};
        json.user = tweet.user;
        json.locations = [];
        
        fs.writeFile(userData + "/" + tweet.user.id + ".json", JSON.stringify(json), 'utf8', function(err) {
          if (err) throw err;
          console.log('complete'); 
        });
      }

      //Appends data to file
      fs.readFile(userData + "/" + tweet.user.id + ".json", function (err, data) {
        var readJSON = JSON.parse(data);
        console.log(readJSON);

        locationJSON = {};
        locationJSON.time = timeStampNow;
        locationJSON.geo = tweet.geo;
        locationJSON.coordinates = tweet.coordinates;
        locationJSON.place = tweet.place;
        
        readJSON.locations.push(locationJSON);
        fs.writeFile(userData + "/" + tweet.user.id + ".json", JSON.stringify(readJSON), 'utf8', function(err) {
          if (err) throw err;
          console.log('complete'); 
        });
      });

    });
脚本的第一部分功能正常,但应将当前位置附加到JSON文件的部分有时会使文件为空,从而导致错误:

undefined
^

SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at ReadFileContext.callback (C:\path\to\Program.js:44:29)
    at FSReqCallback.readFileAfterOpen [as oncomplete] (fs.js:257:13)

fs.writeFile和read file是异步操作。创建文件时,它还可能尝试读取尚未创建的文件,以便获取未定义的数据。在旁注中,检查错误

console.log(“@”+tweet.user.screen_name+“-”+tweet.user.name);
timeStampNow=“[”+date.getDate()+”:“+date.getMonth()+”:“+date.getFullYear()+”-“+date.getHours()+”:“+date.getMinutes()+”:“+date.getSeconds()+”;
console.log(timeStampNow+“”+tweet.place.full_name);
fs.exists(userData+“/”+tweet.user.id+”.json),函数(exists){
//如果文件不存在,则执行
如果(!存在){
console.log(“未识别的人。添加到文件夹”);
json={};
json.user=tweet.user;
locationJSON={};
locationJSON.time=timeStampNow;
locationJSON.geo=tweet.geo;
locationJSON.coordinates=tweet.coordinates;
locationJSON.place=tweet.place;
json.locations=[locationJSON];
fs.writeFile(userData+“/”+tweet.user.id+”.json),json.stringify(json),'utf8',函数(err){
如果(错误)抛出错误;
console.log('complete');
});
}否则{
fs.readFile(userData+“/”+tweet.user.id+”.json),函数(err,data){
var readJSON=JSON.parse(数据);
log(readJSON);
locationJSON={};
locationJSON.time=timeStampNow;
locationJSON.geo=tweet.geo;
locationJSON.coordinates=tweet.coordinates;
locationJSON.place=tweet.place;
readJSON.locations.push(locationJSON);
fs.writeFile(userData+“/”+tweet.user.id+”.json)、json.stringify(readJSON)、'utf8',函数(err){
如果(错误)抛出错误;
console.log('complete');
});
});
}
//将数据追加到文件
});
{
    "user":{
        "id":"877id920012",
        "id_str":"id_str",
        "name":"name",
        "screen_name":"screen_name",
        "location":"location",
        "url":"url",
        "description":"description",
        "translator_type":"translator_type",
        "protected":"protected",
        "verified":"verified",
        "followers_count":"followers_count",
        "friends_count":"friends_count",
        "listed_count":"listed_count",
        "favourites_count":"favourites_count",
        "statuses_count":"statuses_count",
        "created_at":"created_at",
        "utc_offset":"utc_offset",
        "time_zone":"time_zone",
        "geo_enabled":"geo_enabled",
        "lang":"lang",
        "contributors_enabled":"contributors_enabled",
        "is_translator":"is_translator",
        "profile_background_color":"profile_background_color",
        "profile_background_image_url":"profile_background_image_url",
        "profile_background_image_url_https":"profile_background_image_url_https",
        "profile_background_tile":"profile_background_tile",
        "profile_link_color":"profile_link_color",
        "profile_sidebar_border_color":"profile_sidebar_border_color",
        "profile_sidebar_fill_color":"profile_sidebar_fill_color",
        "profile_text_color":"profile_text_color",
        "profile_use_background_image":"profile_use_background_image",
        "profile_image_url":"profile_image_url",
        "profile_image_url_https":"profile_image_url_https",
        "profile_banner_url":"profile_banner_url",
        "default_profile":"default_profile",
        "default_profile_image":"default_profile_image",
        "following":"following",
        "follow_request_sent":"follow_request_sent",
        "notifications":"notifications"
    },
    "locations":[
        {
            "time":"time",
            "geo":"geo",
            "coordinates":"coordinates",
            "place":{
                "id": "id",
                "url": "url",
                "place_type": "place_type",
                "name": "name",
                "full_name": "full_name",
                "country_code": "country_code",
                "country": "country",
                "bounding_box": {
                     "type": "type",
                     "coordinates": "coordinates"
                },
                "attributes": {}
              }
        }
    ]
}