I'm currently working on an application that aims to connect users with friends based on specific questions they answer. However, I keep encountering an error message stating "TypeError [ERR_INVALID_CALLBACK]: Callback must be a function" when the code reaches line 46 in the file. This section of the code utilizes fs to update another file's contents by adding the user-inputted information from the webpage for this application.
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]);
})
})
}
My goal is for this code to modify the friends.js file by incorporating all the information provided by the user through the survey responses. Additionally, it should display the user's closest friend match on the page based on their answers.