简体   繁体   English

如何使用回调函数修复错误?

[英]How do I fix an error with a callback function?

I am trying to make an application that helps a user find a friend based on certain questions that they answered. 我正在尝试开发一个应用程序,以帮助用户根据他们回答的某些问题找到朋友。 I keep coming up with an error that says "TypeError [ERR_INVALID_CALLBACK]: Callback must be a function" when the code gets to line 46 of the file, which uses fs to alter the contents of another file to add the information that the user inputed on the webpage for this application and I have no idea why it is doing this. 当代码到达文件的第46行时,我不断出现一个错误,提示“ TypeError [ERR_INVALID_CALLBACK]:回调必须是一个函数”,它使用fs更改另一个文件的内容以添加用户输入的信息在此应用程序的网页上,我不知道为什么要这样做。

const fs = require('fs');

module.exports = function(app, path) {

    app.get('api/friends', function(req, res) {
        fs.readFile("app/data/friends.js", "utf8", function(err, data) {
            if (err) throw err;

            else {
                res.json(JSON.parse(data));
            }
        });
    });

    app.post('/api/friends', function(req, res) {
        let results = [];

        const postResponse = JSON.stringify(req.body);

        fs.readFile('app/data/friends.js', function (err, data) {
            if (err) throw err; 

            let friendFile = JSON.parse(data);
            console.log(friendFile[0].answers);
            let closestMatch = 0;
            let matchScore = 999999999999999;

            for (let i = 0; i < friendFile.length; i++) {
                console.log(friendFile.length);
                let spaceBetween = 0;
                for (let j = 0; j < friendFile[i].answers.length; j++) {
                    // ['answers[]'][j]
                    console.log(req.body.answers[j]);
                    spaceBetween += Math.abs((parseInt(req.body.answers[j]) - parseInt(friendFile[i].answers[j])));
                }
                if (spaceBetween <= matchScore) {
                    matchScore = spaceBetween;
                    closestMatch == i;
                } 
            }

            results.push(friendFile[closestMatch]);

            friendFile.push(JSON.parse(postResponse));

            fs.writeFile("app/data/friends.js", JSON.stringify(friendFile));
                res.send(results[0]);
        })
    })
}

I expect this to edit the friends.js file to add all of the information from the responses that the user gave on the survey and for it to post the user's closest friend match on the page based on the answers that they gave. 我希望这样可以编辑friends.js文件,以添加用户在调查中给出的回复中的所有信息,并根据用户给出的答案将用户最亲密的朋友匹配发布到页面上。

Just add the callback function when calling writeFile 只需在调用writeFile时添加回调函数

let callback = function(err) {
 if (err) throw err;
  console.log('The file has been saved!');
};
fs.writeFile("app/data/friends.js", JSON.stringify(friendFile), callback);

I'm guessing this is an express app yeah? 我猜这是一个快递应用程序,是吗? Just change.. 只是改变..

fs.writeFile("app/data/friends.js", JSON.stringify(friendFile));

...to... ...至...

fs.writeFile("app/data/friends.js", JSON.stringify(friendFile), function (err) {
  if (err) {
    res.sendStatus(500) // Let the client know we died
    throw err; // Or do something else.
  }

  // If you want to add something for after the file is written to disk put it here.
  // Code execution won't reach this point if the error above throws.
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM