Node JS File empty after read - javascript

I'm Trying to log the location of tweets in a seperate JSON File for each Twitter ID i watch. The following code is called for each tweet and should create a new JSON File for each new ID and append the location of the current 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');
});
});
});
The First part of the Script functions without a problem, but the Part that should append the current location to the JSON File, sometimes makes files empty, resulting in an Error:
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)
Heres a example of how the JSON should look in the end:
{
"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": {}
}
}
]
}

fs.writeFile and read file are async operation. When you create file, it might also try to read the file which has not yet been created so you get undefined data. On the side note , check errors
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;
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', function(err) {
if (err) throw err;
console.log('complete');
});
}else{
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');
});
});
}
//Appends data to file
});
, before perform any operation.

Related

write huge data into a file in crashes node

The node crashes when writing around ~400MB of data into a file.
Here is my code:
function logIntoFile (logObject){
let date = new Date();
let currentDate = date.getFullYear() + '-' + (((date.getMonth() + 1) < 10) ? '0' : '') + (date.getMonth() + 1) + '-' + ((date.getDate() < 10) ? '0' : '') + date.getDate();
let currentHour = ( (date.getHours() < 10 ? '0' : '') + date.getHours() );
let fileName = '/custom-logs/resultSet_' + currentDate + '_' + currentHour + '#' + logObject.dbName;
let logFile = fs.createWriteStream(__dirname + fileName, { flags: 'a' });
logFile.write(util.inspect(logObject, false, null) + '\n');
logFile.end();
}
// doQuery contains logic to connect to DB and run the query provided
doQuery(req, res, sql, false, function(err, result){
if (err) {
...
console.error(err);
res.send(err);
} else {
...
res.send(result);
...
logIntoFile({
timeStamp : getCurrentDateTime(),
dbName : req.session.iss.dbname,
userId : inuserid,
sql : sql,
error : err,
response : result
});
});
logObject variable is an object which is passed from other function and its contains json data.
Here is the error:
It works fine if the writable data is smaller.
P.S. My server has 4 GB of ram.
How to solve this issue? Any help would be highly appreciated.
TIA

array value is not updating in for loop in nodejs

I am trying to push data in an array after downloading the image from download function. It's a problem in nodejs promise. How can I fix this issue.
current output:
[
{
sku: 99104942591,
retailer: 'JCREWFCT',
images: []
}
]
expected output:
[
{
sku: 99103497136,
retailer: 'JCREWFCT',
images: [
"http://localhost:4001/JCREWFCT/99103497136.png",
"http://localhost:4001/JCREWFCT/99103497136_1.png"
]
}
]
in outputArr I am trying to store data
var downloadImages = async function() {
var outputArr = [];
for(var i=0; i<excelJsonArr.length; i++) {
var d = excelJsonArr[i];
var out = {
sku : d.sku,
retailer : d.retailer,
images : []
}
if(d.image_link_1) {
var saveDir = path.join('./public', d.retailer, d.sku+'.png');
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' + properties.get('port') + '/' + d.retailer + '/' + d.sku + '.png';
await download(d.image_link_1, saveDir, function(){
out.images.push(imgUrl);
});
}
if(d.image_link_2) {
var saveDir = path.join('./public', d.retailer, d.sku+'_2.png');
await download(d.image_link_1, saveDir, function(){
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' + properties.get('port') + '/' + d.retailer + '/' + d.sku + '_2.png';
out.images.push(imgUrl);
});
}
outputArr.push(out);
}
console.log(outputArr);
}
var download = async function(uri, filename, callback){
await request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
I don't know what your download-function actually does but normally, when working with asnyc, you would do this:
await download(d.image_link_1, saveDir);
out.images.push(imgUrl);
and I you should try to work with try to catch any errors coming from download, see: Correct Try…Catch Syntax Using Async/Await
FYI, next time, share more of your code and if possible a reproduceable code or example GitHub repo, so we can see the error by ourself.
Since you are using async await you don't need to use callback function. Just call the desired functions after await
var downloadImages = async function () {
var outputArr = [];
for (var i = 0; i < excelJsonArr.length; i++) {
var d = excelJsonArr[i];
var out = {
sku: d.sku,
retailer: d.retailer,
images: []
}
if (d.image_link_1) {
var saveDir = path.join('./public', d.retailer, d.sku + '.png');
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' + properties
.get('port') + '/' + d.retailer + '/' + d.sku + '.png';
await download(d.image_link_1, saveDir, function () {
// out.images.push(imgUrl);// <-- not here
});
out.images.push(imgUrl); // <-- here
}
if (d.image_link_2) {
var saveDir = path.join('./public', d.retailer, d.sku + '_2.png');
await download(d.image_link_1, saveDir, function () {
/* var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' +
properties.get('port') + '/' + d.retailer + '/' + d.sku + '_2.png';
out.images.push(imgUrl); */ // <-- not here
});
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' +
properties.get('port') + '/' + d.retailer + '/' + d.sku + '_2.png';
out.images.push(imgUrl); // <-- here
}
outputArr.push(out);
}
console.log(outputArr);
}

JavaScript - Remove rows dynamically after 2mins

I am calling a webapi using get method which will give json data and I am display as a table from that data. the code which I have given below is working fine. I got around 25 rows(dynamically" of data when I fit the service. this webpage will be refreshed using signalR. when any changes in DB immediately it should reflect in the webpage.
My question is if "" + item.TakeupType + "" value is "Completed" or "cancelled" this row background should change to gray color and after 2mins row should be remove from the webpage.
Note:
1) Only 22 rows should display on webpage in orderby asc Datetime.
2) Completed/cancelled items from DB should will display on the webpage for 2 mins and drop off.
My code :
Hide Expand Copy Code
//Webapi call - To load info status.
function LoadinfoStatus() {
$.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader('x-api-key', '9024024A-024-485C024-6BC2024DA');
}
});
$.ajax({
type: "GET",
url: "https://mywebsites.net/version1/info/494",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// alert(JSON.stringify(data));
$("#DIV").html('');
var DIV = '';
//Function to get DateFormat
function getFormattedDate(date) {
// debugger;
var inputdate = new Date(date);
var currentdate = new Date();
var strTime = getFormattedTime(inputdate);
var rngStrTime = getFormattedTime(add_minutes(inputdate, 5));
if (inputdate > currentdate) {
return inputdate.getDate() + '/' + (inputdate.getMonth() == 0 ? 12 : inputdate.getMonth()) + " " + strTime + " - " + rngStrTime;
}
else {
return strTime + " - " + rngStrTime;
//return day + "/" + month + " - " + strTime;
}
}
var add_minutes = function (dt, minutes) {
return new Date(dt.getTime() + minutes * 60000);
}
function getFormattedTime(inputdate) {
var day = inputdate.getDate();
var month = inputdate.getMonth() + 1;
var hours = inputdate.getHours();
var minutes = inputdate.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
return hours + ':' + minutes + ampm;
}
$.each(data, function (i, item) {
var rows = "<tr ' " + (item.Count > 100 ? "data-id='" + item.orderId + "'" : "") + (item.TakeupType == "Cancelled" ? "style='background-color: gray; color: white'" : "") + " align= 'center' > " +
"" + "" + "" + "" +
"" + item.user.firstName + " " + item.user.lastName.charAt(0) + "." + "" +
"" + item.TakeupType + "" +
"" + (item.Count == undefined ? "$" + " " + 0 : "$" + " " + item.Count) + "" +
"" + getFormattedDate(item.TakeupTimeUtc) + "" +
"= 100 ? "style='background-color: darkorange; color: white'>***" : ">") + "" +
"";
var $tbody = $('tblOrders tbody');
$('#tblOrders').append(rows);
}); //End of foreach Loop
registerEvents();
// console.log(data);
}, //End of AJAX Success function
failure: function (data) {
alert(data.responseText);
}, //End of AJAX failure function
error: function (data) {
alert(data.responseText);
} //End of AJAX error function
});
}

Javascript object Class is not defined Error

I have a mainoops.js file which has following code
var Notifier = function(){
this.socket = io.connect('http://localhost:8080');
this.currentdate = new Date();
}
Notifier.prototype.addCbNotification = function(message){
var datetime = this.currentdate.getDate() + "/" + (this.currentdate.getMonth()+1) + "/" + this.currentdate.getFullYear() + " # " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var datamessage = " <div class='row'><div class='col-xs-2'><i class='fa fa-envelope'></i></div><div class='col-xs-10'><h5><a href=''>" + message + "</a></h5><small>"+datetime+"</small></div></div>";
$("#callback-btn").addClass('alert-notice');
$( "#callback" ).append( "<li class='list-group-item unread'>"+datamessage+" </li> " );
}
Notifier.prototype.addNotification = function(message){
var datetime = currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " # " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var datamessage = " <div class='row'><div class='col-xs-2'><i class='fa fa-envelope'></i></div><div class='col-xs-10'><h5><a href=''>" + message + "</a></h5><small>"+datetime+"</small></div></div>";
$("#notice-btn").addClass('alert-notice');
$( "#notice" ).append( "<li class='list-group-item unread'>"+datamessage+" </li> " );
}
Notifier.prototype.startsocketforcallbback = function(myid) {
// body...
socket.on('callback', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addCbNotification(message[1]);
}
});
};
Notifier.prototype.startsocketfornotice = function() {
// body...
socket.on('notice', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addNotification(message[1]);
}
});
};
I am calling it in my php file as follow
<script src="{{asset('/js/mainoops.js')}}"></script>
But when I try to instantiate it in the PHP page like this
<script>
var obj = new Notifier();
obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
obj.startsocketfornotice();
</script>
I am getting following ERROR
Uncaught ReferenceError: Notifier is not defined
Unfortunately, but this is your callbacks did not points to your Notifier instance. Try to use 3rd parameter of on(..):
socket.on('callback', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addCbNotification(message[1]);
}
},this);
in Notifier.prototype.startsocketforcallbback and
socket.on('notice', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addNotification(message[1]);
}
},this);
in Notifier.prototype.startsocketfornotice
Using jquery :
jQuery(document).ready(function($) {
var obj = new Notifier();
obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
obj.startsocketfornotice();
});

SQL node.js Syntax error on what seems to be a valid query?

I'm running an update on a table to set a position. I've extracted the query and manually run it on my database and works fine but when passed through connection.query() it seems to think there's a syntax error in my node.js console.
function sendShipPosition(position) {
var input = '';
if (position.moving === true) {
var currentdate = new Date();
var datetime = currentdate.getFullYear() + "-"
+ (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
var input = ', moving_datetime = ' + datetime;
}
connection.query('UPDATE ships SET x_axis = :x, y_axis = :y' + input + ' WHERE ship_id = :ship_id'), {
x: parseInt(position.x),
y: parseInt(position.y),
ship_id: 1
};
}
Here is the syntax error:
Here's the input data value of 'position' variable:
{ x: '-605', y: '-257', moving: 0 }
I hope I'm not being too much of a dunce and sorry for the low quality question.
Thanks
This function will generate SQL code which is missing quotes around the datetime variable, resulting in invalid SQL code.
function sendShipPosition(position) {
var input = '';
if (position.moving === true) {
var currentdate = new Date();
var datetime = currentdate.getFullYear() + "-"
+ (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
# Here!
var input = ', moving_datetime = \'' + datetime + '\''
}
connection.query('UPDATE ships SET x_axis = :x, y_axis = :y' + input + ' WHERE ship_id = :ship_id'), {
x: parseInt(position.x),
y: parseInt(position.y),
ship_id: 1
};
}

Categories