i have an application which needs a data.json
file in order to draw a d3-graph. However i need to update that file on an onClick
-Event:
我有一个应用程序,需要一个data.json文件,以绘制一个d3图形。但是我需要在onClick-Event上更新该文件:
d3.select("#updatebutton").on("click", function(e) {
try{
$.get('https://localhost:4444/data', function(data) {
});
}
catch (e) {
alert('Error: ' + e);
}
});
Above is the update-Button with the jquery-call. In my app.js
File I am using it like this:
上面是带有jquery-call的update-Button。在我的app.js文件中,我正在使用它:
app.get('/data', function(req, res, next) {
try{
getJSON();
}
catch(e) {
alert('Error');
}
});
The getJSON()
-Function is received Data over an https-Request, processes that data and saves it to data.json
:
getJSON() - 函数通过https-Request接收数据,处理该数据并将其保存到data.json:
function getJSON() {
var req = https.get(options, function(response) {
// handle the response
var res_data = '';
response.on('data', function(chunk) {
res_data += chunk;
});
response.on('end', function() {
//process data
// save to file
fs.writeFile(filePath, JSON.stringify(finalJson), function(err) {
if (err)
throw err;
});
});
});
}
However if i click on my updateButton
repeatedly after seconds, it seems that data.json
is not overwritten but the file gets bigger and bigger, means that data is added to the file instead of overwritten.
但是,如果我在几秒钟后反复点击我的updateButton,似乎data.json没有被覆盖,但文件变得越来越大,意味着数据被添加到文件而不是被覆盖。
What am I doing wrong?
我究竟做错了什么?
Thanks for help.
感谢帮助。
1 个解决方案
#1
2
Since you use app.get as your route, I guess you are using express.
由于您使用app.get作为路线,我猜您使用快递。
In your routes definition:
在您的路线定义中:
var getData = (function() {
var callbacks = [];
function executeCallbacks(err, data) {
for (var i = 0; i < callbacks.length; i++) {
callbacks[i](err, data);
}
callbacks = [];
}
return function(cb) {
callbacks.push(cb);
if( callbacks.length === 1 ) {
var req = https.get(options, function(response) {
// handle the response
var res_data = '';
response.on('data', function(chunk) {
res_data += chunk;
});
response.once('end', function() {
// process data here
// save to file
fs.writeFile(filePath, JSON.stringify(finalJson), function(err) {
if (err) {
// call error handler
return executeCallbacks(err);
}
executeCallbacks(null, body);
});
});
response.once('error', function() {
return executeCallbacks(err);
});
}
req.end();
}
};
})();
app.get('/data', function(req, res, next) {
getData(function(err, data) {
if(err) {
return next(err);
}
return data;
});
});
In your browser js file:
在您的浏览器js文件中:
d3.select("#updatebutton").on("click", function(e) {
$.get( 'https://localhost:4444/data', function(data) {
alert( "success" );
var json = JSON.parse(data);
})
.fail(function() {
alert( "error" );
});
});
I see you use try / catch around callback functions. The callback function fires after the original function completes. So don't use Try / Catch around callback function.
我看到你在回调函数中使用try / catch。原始函数完成后将触发回调函数。所以不要在回调函数周围使用Try / Catch。
Read: https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/
#1
2
Since you use app.get as your route, I guess you are using express.
由于您使用app.get作为路线,我猜您使用快递。
In your routes definition:
在您的路线定义中:
var getData = (function() {
var callbacks = [];
function executeCallbacks(err, data) {
for (var i = 0; i < callbacks.length; i++) {
callbacks[i](err, data);
}
callbacks = [];
}
return function(cb) {
callbacks.push(cb);
if( callbacks.length === 1 ) {
var req = https.get(options, function(response) {
// handle the response
var res_data = '';
response.on('data', function(chunk) {
res_data += chunk;
});
response.once('end', function() {
// process data here
// save to file
fs.writeFile(filePath, JSON.stringify(finalJson), function(err) {
if (err) {
// call error handler
return executeCallbacks(err);
}
executeCallbacks(null, body);
});
});
response.once('error', function() {
return executeCallbacks(err);
});
}
req.end();
}
};
})();
app.get('/data', function(req, res, next) {
getData(function(err, data) {
if(err) {
return next(err);
}
return data;
});
});
In your browser js file:
在您的浏览器js文件中:
d3.select("#updatebutton").on("click", function(e) {
$.get( 'https://localhost:4444/data', function(data) {
alert( "success" );
var json = JSON.parse(data);
})
.fail(function() {
alert( "error" );
});
});
I see you use try / catch around callback functions. The callback function fires after the original function completes. So don't use Try / Catch around callback function.
我看到你在回调函数中使用try / catch。原始函数完成后将触发回调函数。所以不要在回调函数周围使用Try / Catch。
Read: https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/