So my goal is to pull info from the database when a user logs in to have it in the URL for when they are logged into their account, I can get the information I am looking for but when I go to log in to the account I am getting this error
Cannot GET /main-component[object%20Object]
and this in the URL
http://localhost:3000/main-component[object%20Object]
I have tried different variations of writing my code and no matter what whenever I go to put the info into the redirect link I just get an error. I am connected to the database and getting data just not able to redirect to the next place on the website.I am new to nodejs so I'm not exactly sure what to do from this point. Thanks for the help and heres my code
let str
let testi
function output(rows){
str = rows
console.log(str)
return str
}
app.post('/login-validate', (req, res) =>{
// let user = userInfo(req.body.email, req.body.password)
// console.log(user)
testi = pool.query('SELECT * FROM users WHERE email=? AND password=?',[req.body.email, req.body.password], (err, res) =>{
if(err){
console.log(err)
}
output(res[0].first_name)
})
res.redirect('/main-component' + testi)
})
app.get('/main-component/:id', (req,res) =>{
res.render('calanderView')
})
Related
i have created one dropdown that passes data to NodeJS through axios post i can see that in console log of nodejs Post but cant able to use the value outside the post function
i want to use the value to querying the database
my nodejs code:
app.post('/getmodel', (req, res) => {
var model = req.body.model;
console.log(model);
//It shows model value here but can't able to use outside
});
app.get('/model', (req,res)=>{
let model = req.body.model;
let sql ="select * from new_schema.model_list,new_schema.images where model_name= " + mysql.escape(model)
db.query(sql,model, (err,results) =>{
if(err){
throw err
}
console.log(results)
res.send(results);
})
})
my react code works fine as i can able to see the selected value in nodejs console below
these are the selected value from my dropdown that shows in my nodejs console. but in cant use it by req.body like that please help me
You can try like this
app.post('/getmodel', (req, res) => {
var model = req.body.model;
console.log(model);
//It shows model value here and you can able to use in the query
let sql = "select * from new_schema.model_list,new_schema.images where model_name= " + mysql.escape(model)
db.query(sql, model, (err, results) => {
if (err) {
throw err
}
console.log(results)
res.send(results);
})
});
I made todo react app where user clicks on delete button it fetches delete request to my node.js server.
const deleteTodo = (id) => {
const q = `DELETE FROM todos WHERE id=${id}`;
connection.query(q, (err, results) => {
if (err) throw err;
console.log('delete', results)
})
}
However, when I simply went with router option below,
app.delete('/', function (req, res) {
(some function)
})
app wouldn't delete just like this.
On the other hand, by simply making it
app.delete('/todos/:id', function (req, res) {
(some function)
})
it will start functioning again.
Why is this so?
I think your question has some missing information.
In particular, how you're calling deleteTodo and what the error actually is.
If I were to make a guess, when /todos/:id is specified, it becomes possible to use req.params.id.
However, when it's /, req.params.id is undefined.
When req.params.id is undefined, the query becomes invalid.
Refer to express documentation for more details.
got a problem with simple user Login application. There goes the code.
exports.postLogIn = (req,res,next) =>{
const reqName = req.body.name;
const reqPassword = req.body.password;
console.log(reqName,reqPassword);
db.query('SELECT * FROM userinfo WHERE name = ? AND password = ?', [reqName, reqPassword], function(error, results, fields) {
console.log(results.toString());
console.log(error);
console.log('qirwoe');
res.end();
});
// User.userRequest(reqName,reqPassword, userInfo =>{
// console.log();
// if(userInfo.length>0){
// console.log('fail');
// // return res.render('user/fail')
// }
// console.log('aproved!');
// // return res.render('user/index',{user:userInfo,pageTitle:'Social Reporter',path:'/'})
// res.redirect('/');
// });
}
So, its not working at all, I dont get any response from console.logs or anything.
Here's what I observed,
SELECT *
FROM userinfo
WHERE name = 'Gui' AND password = '123465'
running this code on mySQL does work. I get a response back on mySQL workbench.
I can INSERT things normally on my DB. So the connection is working.
I did get the right req.body, so the input on '?' mysql command is right
I did the following command to import mysql 'npm install --save mysql2'
Anyway, dont know what else to do, let me know if I need to provide more info. Thanks for your all attention!
Problem solved!
By those who's having the same problem I will post my answer here.
So, by some researches I saw that it may be an async problem, and by #esqew comment as well.
Then I tried to remove those lines,
db.query('SELECT * FROM userinfo WHERE name = ? AND password = ?', [reqName, reqPassword], function(error, results, fields) {
console.log(results.toString());
console.log(error);
console.log('qirwoe');
res.end();
});
Which is the problem, and repleace to an async function:
async function loginAttempt () {
let [rows, fields] = await db.execute('SELECT * FROM userinfo WHERE name = ? AND password = ?', [reqName, reqPassword]);
console.log(rows);
}
loginAttempt();
Problem solved, be aware to sync/async funcions in mySQL querys. Again, correct me if I'm wrong with any information.
Hope I could help anyone.
I'd like to create api call from back-end for DELETE query from mysql DB but when execute it in browser get error
'Cannot GET ...'
I pass into the route id of row which had got from DB
At back-end the code is:
app.delete('/products/delete/:id*?', function(req, res) =>{
let { id } = req.query;
let DELETE_PRODUCT_FROM_DB = `DELETE FROM my_db.products WHERE my_db.id= '${req.query}'`;
console.log("id: ", req.query);
// delete a row with id = req.query
connection.query(DELETE_PRODUCT_FROM_DB, (error, results, fields) => {
if (error) return console.error(error.message);
res.status(200).send(results);
console.log("Deleted Row(s):", results.affectedRows);
});
});
But finally this call not works and row not deleted
let DELETE_PRODUCT_FROM_DB = `DELETE FROM my_db.products WHERE my_db.id= '${req.query.id}'`;
console.log("id: ", req.query.id);
Try using this.
fetch(url, {
method: 'delete'
}).then(response => response.json());
Try running this in your browser console. It should work.
Most likely you're making a GET call to a DELETE resource.
Please read Express 4.x. Can you share the code you're using to make DELETE request from browser?
I did some changes and now running version of the code looks like
app.delete('/products/delete/:id', (req, res) => {
let { id } = req.params ;
let DELETE_PRODUCT_FROM_DB = `DELETE FROM my_DB.products WHERE id= '${id}'`;
console.log('id: ', req.params);
// delete a row with id = req.params
connection.query(DELETE_PRODUCT_FROM_DB, (error, results, fields) => {
if (error) return console.error(error.message);
res.status(200).send(results);
console.log('Deleted Row(s):', results.affectedRows);
});
});
Also, I figured out that changes from req.query on req.params helped to get id from the link as a parameter
Here is my get request made to a mysql table
app.get('/', (req, res) => {
let sql = 'SELECT * from emarttesttable WHERE id = 229';
let query = db.query(sql, (err, results) => {
if(err){console.log(err);}
else{
console.log(results);
}
});
res.render('index');
});
As it stands, this function allows me to grab the information I want from the table and I can read the results via console.log. However, I'm unable to access results on my index.ejs page.
How do I access results(which is an object that contains the stuff I want) in my index.ejs file? Whenever I try to access results, it says that results in undefined. How do I make sure that the object that is created as a result of the call to the table is able to be used/accessed on a different page. For the time being, I would just like to create a simple table that has the keys in one column and the values in a second column.
You need to modify your code as below. The reason is db.query is an async operation and you are trying to render before the async request completed. Also, to be able to reach the result at your template engine, you need to pass the results to the render. (index.ejs)
app.get('/', (req, res) => {
let sql = 'SELECT * from emarttesttable WHERE id = 229';
let query = db.query(sql, (err, results) => {
if(err){console.log(err);}
else{
res.render('index', results);
console.log(results);
}
});