在JavaScript函数中从SQL查询中检索变量(重写)
retrieve variables from SQL query in JavaScript function (rewrite)
我在Apache Cordova收集数据并将其发送到web服务器的移动应用程序工作。我想通过main函数
来实现重写代码,使其更集中、更清晰
//main function launched by a button
function sendData(){
//set all var as suggested
var content = '';
var photoList = [];
var fileName = '';
//call children functions
generateCsv(); // 1st child function
saveCSV(); // 2nd child function
uploadFile(); // 3rd child function
}
第一个函数从数据库中提取数据,并为上面的var
设置新值function generateCsv() { // 1st child function
db.transaction(function(tx) {
tx.executeSql(selectAllStatement, [], function(tx, result) {
//do something I do not understand really... but work!
// but set value to var!!
console.log(content); //the new value is set correctly!!
});
});
}
现在,如果我尝试在主函数中的下一个函数中记录我的三个var的值,我不会收到generateCsv函数所做的更改…
function saveCSV() { // 2nd child function
console.log(fileName); //----not updated
console.log(content); //----not updated
console.log(photoList);//----not updated
//do something with my vars!
}
最后一个子函数将上传文件
function uploadFile(){ 3rd child function
//do something and upload....
}
代码完整:
//------------------SEND DATA----------------->>
function sendData(){
var content = 'id,wateres,source,s_date,latitude,longitude,top,pump,pump_manual,other_pump,operator,operator_type,condition,disused,abandoned,access,water_presence,drinking_water,fee,fixme,note,photo,exif'n';
var fileName = '';
var date = new Date().toString();
var photoList = [];
generateCsv();
}
//---------------GENERATE CSV FILE--------------------->>
function generateCsv() {
db.transaction(function(tx) {
tx.executeSql(selectAllStatement, [], function(tx, result) {
dataset = result.rows;
if (dataset.length!=0){
for (var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
//uploadPhoto(item['photo']);
photoList.push(item['photo']);
var exif = item['exif'].replace(/('r'n|'n|'r)/gm, " | ");
exif = exif.replace(/,/g, '.');
content += item['id']+ ','
+ item['wateres'] + ','
+ item['source'] + ','
+ item['s_date'] + ','
+ item['lat'] + ','
+ item['lon'] + ','
+ item['top'] + ','
+ item['pump'] + ','
+ item['pump_manual'] + ','
+ item['other_pump'] + ','
+ item['operator'] + ','
+ item['operator_type'] + ','
+ item['condition'] + ','
+ item['disused'] + ','
+ item['abandoned'] + ','
+ item['access'] + ','
+ item['water_presence'] + ','
+ item['drinking_water'] + ','
+ item['fee'] + ','
+ item['fixme'] + ','
+ item['note'] + ','
+ item['photo'] + ','
+ exif + ''n';
fileName = item['source'] + '|' + date;
}
fileName = fileName.replace(/' /g, "_");
fileName = fileName.replace(/',/g, "");
fileName = fileName.replace(/':/g, "-")+'.csv';
$('#message').html('<p><strong>Data have been collected.</strong> </p>');
saveCSV();
}
});
});
}
//--------------------------SAVE CSV ------------------
function saveCSV() {
console.log(fileName);
var fileObject;
document.addEventListener("deviceready", onDeviceReady, true);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, onFileSystemSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(fileName, { create: true, exclusive: false },
gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileObject = fileEntry;
$('#saveFile_csv').on('click', function() {
saveFileContent();
//uploadFile(fileName);
});
}
function saveFileContent() {
fileObject.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
var myText = document.getElementById('my_text').value + content;
writer.write(myText);
writer.onwriteend = function(evt) {
$('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>');
var reader = new FileReader();
};
}
function fail(error) {
alert('fail to write file code = ' + error.code);
}
}
//----------------UPLOAD-CSV------------------------>>
function uploadFile(fileName) {
var fileURL = "///storage/emulated/0/Android/data/com.app/cache/"+fileName;
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
$('#contents').html('<strong>File uploaded</strong><br>'+ new Date() );
}
function fail(error) {
alert("An error has occurred uploading file: Code = " + error.code);
console.log("upload error source: " + error.source);
console.log("upload error target: " + error.target);
}
var uri = encodeURI("http://www.website.com/upload.php");
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=fileURL.substr(fileURL.lastIndexOf('/')+1);
options.mimeType="text/plain";
var headers={'headerParam':'headerValue'};
options.headers = headers;
var ft = new FileTransfer();
ft.upload(fileURL, uri, win, fail, options);
$('#contents').html('<strong>Whait upload confirmation...</strong>');
}
谢谢! !
你只需要设置函数外的所有变量,如
var content = '';
var fileName = '';
var date = new Date().toString();
var photoList = [];
然后,当你给变量赋值时你必须使用全局变量,比如
function setVarItem()
{
content = "test string here or value here";
}
现在在另一个函数中检索该变量值,如
function getVarItem()
{
alert(content) //output: test string here or value here
}
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 重写JS中函数内部的变量
- 函数正在重写变量.这怎么可能
- 如何访问在公共函数中重写的公共函数中的私有变量
- Javascript(JQuery)变量重写问题
- JavaScript中的命名空间:我需要重写所有的变量和函数引用吗
- 在Razor视图中重写变量
- 重写使用局部变量的节点模块方法(NodeJS)
- 为什么JS在重写变量时会抛出意外错误?
- 在AngularJS中重写变量
- 重写一个变量,旧的引用发生了什么
- 在Angular 1.3.15中重写第三方指令中的变量
- 在JavaScript函数中从SQL查询中检索变量(重写)
- 如何访问'这'如果原型函数中的变量被重写
- 重写全局变量并在另一个函数javascript中返回
- 加载具有与其他脚本相同的变量的脚本并重写
- 重写javascript函数中的变量