Javascript/Node/JSON问题,为什么这不起作用?
我以为我明白我在做什么,直到事情不顺为止。我正在通过节点运行此操作,而不是通过浏览器 它首先进入while循环末尾的提示符。我不知道为什么Javascript/Node/JSON问题,为什么这不起作用?,javascript,node.js,json,Javascript,Node.js,Json,我以为我明白我在做什么,直到事情不顺为止。我正在通过节点运行此操作,而不是通过浏览器 它首先进入while循环末尾的提示符。我不知道为什么 const fs = require('fs'); const prompt = require('prompt-sync')(); function jsonReader(filepath, cb){ fs.readFile(filepath, 'utf-8', (err, fileData) => { if (err) {
const fs = require('fs');
const prompt = require('prompt-sync')();
function jsonReader(filepath, cb){
fs.readFile(filepath, 'utf-8', (err, fileData) => {
if (err) { return cb && cb(err); }
try {
const object = JSON.parse(fileData);
console.log(object);
return cb && cb(null, object);
} catch (err) {
return cs && cb(err);
}
});
}
var exit = 0;
do {
jsonReader('./customer.json', (err, customer) => {
if (err) {
console.log('Error reading file:',err)
return
}
//customer.order_count +=1
const note = prompt("Enter a note for the JSON file: ");
customer.note = note;
fs.writeFile('./customer.json', JSON.stringify(customer, null, 2), (err) =>{
if (err) {
console.log('Error writing file:',err)
} else {
console.log('File updated');
}
})
})
exit = prompt("Do you want to exit?");
} while (exit != 'y');
这是因为
fs.readFile
是异步的,所以第二个提示是在fs.readFile
完成之前执行的。您可能希望使用异步函数并等待或将提示放在jsonReader
的回调末尾,这是因为fs.readFile
是异步的,所以第二个提示是在fs.readFile
完成之前执行的。您可能希望使用异步函数,并等待或将提示放在jsonReader
的回调末尾。您的代码有几处错误。您注意到的提示问题只是一个开始
代码的执行方式如下所示:
// happens now
do {
// happens now
// ...
jsonReader('./customer.json', (err, customer) => {
// happens after readFile
fs.writeFile('./customer.json', JSON.stringify(customer, null, 2), (err) =>{
// happens after writeFile
// ...
})
// happens after readFile
// ...
})
// happens now
exit = prompt("Do you want to exit?");
} while (exit != 'y');
// happens now
时间顺序如下:
1. Things that happens now
2. Things that happens after readFile
3. Things that happens after writeFile
很明显,您是在readFile或writeFile之前输出提示
然而,还有另一个问题。您有一个无限的while
循环。在node.js中,事实上在浏览器中,I/O只在没有javascript可执行时发生——换句话说,它发生在解释器空闲时。您使用while循环阻止脚本到达脚本末尾,因此解释器从不空闲
对于您的脚本,修复提示后将发生的问题是:
1. Things that happens now
2. loop into things that happens now
3. loop into things that happens now
4. loop into things that happens now
5. loop into things that happens now
..
∞. loop into things that happens now
因此,readFile和writeFile永远不会执行。您需要使用递归异步调用或使用setTimeout()
或setInterval()
替换while循环,或者使用带有async/await的while循环
下面是对代码进行最小更改的实现:
function doIt () { // <----------- replace the do..while loop
// ...
jsonReader('./customer.json', (err, customer) => {
// ...
fs.writeFile('./customer.json', JSON.stringify(customer, null, 2), (err) =>{
// ...
var exit = prompt("Do you want to exit?");
if (exit !== 'y') {
doIt(); // <-------------- repeat the process again
}
})
})
}
doIt(); // <--------- don't forget to begin the whole thing
函数doIt(){//{
// ...
fs.writeFile('./customer.json',json.stringify(customer,null,2),(err)=>{
// ...
var exit=提示(“是否要退出?”);
如果(退出!='y'){
doIt();//您的代码有几处错误。您注意到的提示问题只是开始
代码的执行方式如下所示:
// happens now
do {
// happens now
// ...
jsonReader('./customer.json', (err, customer) => {
// happens after readFile
fs.writeFile('./customer.json', JSON.stringify(customer, null, 2), (err) =>{
// happens after writeFile
// ...
})
// happens after readFile
// ...
})
// happens now
exit = prompt("Do you want to exit?");
} while (exit != 'y');
// happens now
时间顺序如下:
1. Things that happens now
2. Things that happens after readFile
3. Things that happens after writeFile
很明显,您是在readFile或writeFile之前输出提示
然而,还有另一个问题。你有一个无限的while
循环。在node.js中,事实上在浏览器中,I/O只在没有javascript可执行时发生——换句话说,它发生在解释器空闲时。你阻止脚本通过while循环到达脚本末尾,因此解释器是nev呃闲置
对于您的脚本,修复提示后将发生的问题是:
1. Things that happens now
2. loop into things that happens now
3. loop into things that happens now
4. loop into things that happens now
5. loop into things that happens now
..
∞. loop into things that happens now
因此,readFile和writeFile永远不会执行。您需要使用递归异步调用或使用setTimeout()
或setInterval()
替换while循环,或者使用带有async/await的while循环
下面是对代码进行最小更改的实现:
function doIt () { // <----------- replace the do..while loop
// ...
jsonReader('./customer.json', (err, customer) => {
// ...
fs.writeFile('./customer.json', JSON.stringify(customer, null, 2), (err) =>{
// ...
var exit = prompt("Do you want to exit?");
if (exit !== 'y') {
doIt(); // <-------------- repeat the process again
}
})
})
}
doIt(); // <--------- don't forget to begin the whole thing
函数doIt(){//{
// ...
fs.writeFile('./customer.json',json.stringify(customer,null,2),(err)=>{
// ...
var exit=提示(“是否要退出?”);
如果(退出!='y'){
doIt();//在服务器端代码中,它是先执行第一个提示还是先执行第二个提示prompt
?在服务器端代码中,它是先执行第一个提示还是先执行第二个提示prompt
?谢谢!我最后返回并以readfilesync和writefilesync的形式执行。我对异步的东西还是新手。谢谢!我我已经准备好返回并以readfilesync和writefilesync的形式执行它。我对异步的东西还是新手。