Here is my server.js:
const express = require('express');
const app = express();
const fs = require('fs');
const bodyParser = require('body-parser');
app.set('view engine', 'ejs')
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Alternatively, read the combinations from another route
const combinations = require('./routes/combinations');
app.get("/", (req, res) => {
res.render('index')
})
app.get("/contact", (req, res) => {
res.render('contact')
})
app.post('/like', (req, res) => {
const votes = req.body.votes;
const Comb_Id = req.body.Comb_Id;
console.log(votes)
console.log(Comb_Id)
const matchedId = combinations.find(c => c.Id === Comb_Id);
console.log(matchedId)
if(matchedId) {
matchedId.vote = votes;
}
else {
console.log("didn't work");
}
});
app.post('/dislike', (req, res) => {
const votes = req.body.votes;
const Comb_Id = req.body.Comb_Id;
const matchedId = combinations.find(c => c.Id === Comb_Id);
if(matchedId) {
matchedId.vote = votes;
}
else {
console.log("didn't work");
}
});
// Set up the endpoint to handle form submissions
app.post('/', (req, res) => {
const dropdown1 = req.body.dropdown1;
const dropdown2 = req.body.dropdown2;
const dropdown3 = req.body.dropdown3;
const dropdown4 = req.body.dropdown4;
// Find the combination in the file or from the route that matches the user's selection
const matchedCombination = combinations.find(c => c.dropdown1 === dropdown1 && c.dropdown2 === dropdown2 && c.dropdown3 === dropdown3 && c.dropdown4 === dropdown4);
if (matchedCombination) {
// If there's a match, send the corresponding result
var output = JSON.stringify(matchedCombination.result);
var user_votes = JSON.stringify(matchedCombination.vote);
var comb_Id = JSON.stringify(matchedCombination.Id);
//res.json({ result: matchedCombination.result });
console.log(output)
console.log(user_votes)
console.log(comb_Id)
res.render('index2', { story: output, dropdown1, dropdown2, dropdown3, dropdown4, scrollToResult: true, votes:user_votes, Id:comb_Id });
} else {
// If there's no match, send an error message
//res.status(400).json({ error: 'Invalid combination' });
res.status(400).render('index2', { story: 'Invalid combination', dropdown1, dropdown2, dropdown3, dropdown4, scrollToResult: true, votes:user_votes, Id:comb_Id });
}
});
app.listen(8000, () => {
console.log('Server started on port 8000');
});
Here is combinations.js file:
const combinations = [
{ dropdown1: 'A', dropdown2: 'F', dropdown3: 'L', result:1, Id:100, vote:0},
{ dropdown1: 'A', dropdown2: 'F', dropdown3: 'M', result:2, Id:200, vote:0},
{ dropdown1: 'A', dropdown2: 'F', dropdown3: 'N', result:3},
{ dropdown1: 'A', dropdown2: 'F', dropdown3: 'O', result:5}
]
module.exports = combinations;
in the server.js file, I am getting the proper result for “matchedCombination” but I am getting undefined for “matchedId” and I am not finding any solution or the issue here.
some more details: I am calling the matchedcombination variable to get the value from combinations.js when the user submits a dropdown form. This flow is working as expected and I am getting the results.
When user has the results in front of him, he can either like or dislike it. To do this, I have added an ID field in the combinations.js. When user submits the dropdown to get the output, I pass this Id to my HTML. When user clicks on like or dislike button, this ID is passed back to the server from the HTML.
I did this to identify the combination of dropdown options the user selected. Using this ID now I want to fetch and update the vote field in combinations.js. That is the plan.