Answer the question
In order to leave comments, you need to log in
Why is incorrect (outdated) data coming to the server?
There is a request handler from the server, the task of which is to call the function I wrote, which in turn will overwrite the json file, which should be further sent to the server.
Server part
router.post('/currentDir1', (req, res) =>{
console.log("POST");
const fs = require('fs');
let body = "";
let pathToFile = "";
console.log(req.body);
req.on("data", function (data) {
body += data;
});
req.on("end", function(currentData) {
console.log(JSON.parse(body));
currentData = JSON.parse(body);
console.log("еще тут");
async function takeValue(){
if(currentData.sizeOrType === "<папка>"){
let dir = currentData.dir + currentData.fileName;
// dir = "C:\\totalcmd";
console.log(dir);
if(currentData.whichScreen){
console.log(currentData.whichScreen);
let result = await foo(dir, './data/firstScreen.json');
pathToFile = './data/firstScreen.json';
var json1 = JSON.parse(fs.readFileSync('./data/firstScreen.json', 'utf8'));
if(result){
console.log("таку хули");
console.log(json1);
res.json(json1);
}
}else{
console.log('aaaa');
foo(dir, './data/secondScreen.json');
pathToFile = './data/firstScreen.json';
res.sendFile(path.resolve('./data/secondScreen.json'));
}
}
}
console.log("я тут");
takeValue();
});
})
const fs = require('fs');
var Foo = async function(dir, pathToFile){
let Mode = require('stat-mode'); // проверка дириктори или файл
let temp; //вспомогательная переменная
let jsonFile = {
table: []
};
console.log("FOO WAS STARTED");
fs.readdir(dir, function(err, items) { // Вроде работает :)
for (let i=0; i<items.length; i++) { //массив всех файлов текущей директории
try{
fs.stat(dir +"\\"+ items[i], function(err, stats){
// console.log(dir +"\\"+ items[i]);
try{
let mode = new Mode(stats); // определяем папка или файл
if(mode.isDirectory()){
temp = "<папка>";
} else{
temp = stats.size.toString() + " байт";
}
jsonFile.table.push({icon:i, fileName:items[i], sizeOrType: temp , dateOfChange: stats.mtime, dir: dir}); //добавляем данные
fs.writeFile(pathToFile, JSON.stringify(jsonFile), (err)=>{
if(err) console.log("error");
});
}
catch(e){
console.log(e);
}
});
}
catch(e){
console.log(e);
continue;
}
}
});
return true;
}
methods:{
clickOnRow: function(elem, whichScreen){
this.clicks++
if(this.clicks === 1) {
var self = this
this.timer = setTimeout(function() {
console.log("одинарный");
self.clicks = 0
}, this.delay);
} else{
clearTimeout(this.timer);
console.log("двойной");
console.log(elem);
elem['whichScreen'] = whichScreen;
// console.log(this.helper);
// this.nameOfMethod(elem);
this.clicks = 0;
fetch('/currentDir1',{
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(elem)
})
// .then(response => console.log(response))
.then(response => response.json())
.then(json => this.helper = json.table)
.then(json => console.log(json))
.then(this.$emit("newvalue", this.helper, whichScreen))
Answer the question
In order to leave comments, you need to log in
Did you think you wrote a word async
and it suddenly became asynchronous by itself? Nothing like this. You have everything scattered into a bunch of parallel executable functions, and no one is waiting for anyone. Learn async in javascript or use synchronous functions (with postfix Sync
).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question