Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/Node/JSON问题,为什么这不起作用?_Javascript_Node.js_Json - Fatal编程技术网

Javascript/Node/JSON问题,为什么这不起作用?

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) {

我以为我明白我在做什么,直到事情不顺为止。我正在通过节点运行此操作,而不是通过浏览器

它首先进入while循环末尾的提示符。我不知道为什么

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的形式执行它。我对异步的东西还是新手。