Node.js - MySQL API, multi GET functions - mysql

I'm new in making API. I use Node.js and MySQL.
The fact is I have two GET function to get all users and one to get user by ID.
Both function are working when they are alone implemented. If both of them are implemented the function to get all user try to enter in the function to get user by ID so the API crash.
So here is my model users.js
var connection = require("../connection");
function Users()
{
//GET ALL USERS
this.get = function(res)
{
console.log('Request without id');
connection.acquire(function(err, con)
{
con.query('SELECT * FROM users', function(err, result)
{
con.release();
if (err)
res.send({status: 1, message: 'Failed to get users'})
else
res.send(result);
});
});
}
//GET USER BY ID
this.get = function(id, res)
{
console.log('Request with ID');
connection.acquire(function(err, con)
{
if (id != null)
{
con.query('SELECT * FROM users WHERE id = ?', id, function(err, result)
{
con.release();
if (err)
res.send({status: 1, message: 'Failed to find user: ' + id});
else if (result == "")
res.send({status: 1, message: 'Failed to find user: ' + id});
else
res.send(result);
});
}
});
}
And here is the routes.js
var users = require('./models/users');
module.exports = {
configure: function(app) {
app.get('/users/', function(req, res) {
users.get(res);
});
app.get('/users/:id/', function(req, res) {
users.get(req.params.id, res);
});
Do you have any idea why ?
Thanks for help :)

You can't have two functions with the same name in the same scope.
You have to rename your functions
/**
* Get all users
*/
this.get = function(res) {...}
/**
* Get user by id
*/
this.getById = function(id, res) {...}
Or you can have one function and check if an id is provided
this.get = function(id, res) {
if ( Number.isInteger(id) ) {
// return the user
} else {
res = id;
// return all users
}
}

Related

Retrieve information upon registration

How can i do this, When a user registers , I would like the endpoint to still go ahead and get back the information which is saved inside the database.For some reason, it does not work as expected
How do i go about this :
My code is looking thus :
app.post("/api/sign-up", async function (req, res) {
dbConn.query(
`select * from accounts where email = ${dbConn.escape(req.body.email)}`,
async function (err, result, fields) {
if (result.length === 0) {
var email = req.body.email;
var phone = req.body.phone;
var password = req.body.password;
var fullname = "NULL";
const hashPass = await bcrypt.hash(password, 12);
dbConn.query(
`insert into accounts(email, phone, password, fullname) values (?,?,?,?)`,
[email, phone, hashPass, fullname],
function (error, results, fields) {
if (error) throw error;
return res.send({
error: false,
data: results[0],
message: "User created Successfully",
});
}
);
} else {
return res.send({
error: true,
message: "User exists",
});
}
}
);
});
Checked thru the internet, i could not find the information needed.
I managed to fix it.
Code looks like this now , and it shows the data inside POST man
app.post("/api/sign-up", async function (req, res) {
dbConn.query(
`select * from accounts where email = ${dbConn.escape(req.body.email)}`,
async function (err, result, fields) {
if (result.length === 0) {
var email = req.body.email;
var phone = req.body.phone;
var password = req.body.password;
var fullname = "NULL";
const hashPass = await bcrypt.hash(password, 12);
dbConn.query(
`insert into accounts(email, phone, password, fullname) values (?,?,?,?)`,
[email, phone, hashPass, fullname],
function (error, results, fields) {
if (error) throw error;
return res.send({
error: false,
email:email,phone:phone,
message: "User created Successfully",
});
//return res.status(201).json({message: 'User created Successfully', "email":email,"phone":phone});
}
);
} else {
return res.send({
error: true,
message: "User exists",
});
}
}
);
});
Thanks to everyone who decided to take a Look :)

go to 404 if route is undefined node.js

I am basically trying to say, if someone types into the browser, xxx.com/homy, instead of xxx.com/home, how do I redirect them to a 404 page? here's my index.js file. I am using node.js
// Direct to View Registrations
router.get('/viewRegistration', auth.ensureAuthenticated, function(req, res, next) {
var adminActive = ""
UtilRole.roleCheck(req, res, 'ADMIN', (response) => {
adminActive = response != undefined ? response : false
const user = JSON.parse(req.session.passport.user)
var query = "SELECT * FROM table WHERE email = '" + user.emailAddress + "'";
ibmdb.open(DBCredentials.getDBCredentials(), function(err, conn) {
if (err) return res.send('sorry, were unable to establish a connection to the database. Please try again later.');
conn.query(query, function(err, rows) {
if (err) {
Response.writeHead(404);
}
for (var i = 0; i < rows.length; i++) {
console.log(rows[i])
}
res.render('viewRegistration', {
page_title: "viewRegistration",
data: rows,
user,
role: adminActive
});
return conn.close(function() {
console.log('closed /viewRegistration');
});
});
});
})
})
module.exports = router;

How to fix the node.js code to check for login

I am new to node js and dont know why is this returning the false value always. I have form made in html that sends post request then the app.js calls login.js function and checks the database
I tried to manually input with 2 var and it works but when using the form it doesnt.
app.post('/login', urlencodedParser, function (req, res) {
var check = require('./js/login.js');
var username = req.body.username;
var password = req.body.password;
if(check([username,password]) == true){
console.log('fine');
res.render('index');
}
else{
console.log('get lost');
res.render('login');
}
});
This is the app.js code thet calls the login.js but the page doesnt render also even if the returned value is incorrect.
var check = function(arr){
var con = require(__dirname + '/database.js');
var login_check = false;
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM users WHERE (name = ?) AND (password = ?)", [arr[0], arr[1]], function (err, result, fields) {
if (err) throw err;
if(result)
{
login_check = true;
}
});
});
return login_check;
};
module.exports = check;
Because of your con.query function is asynchronous, so the return of function will always be false by default (it returns before executing check). Another way, you can try to use callback function as my suggestion code below:
app.post("/login", urlencodedParser, function(req, res) {
var check = require("./js/login.js");
var username = req.body.username;
var password = req.body.password;
var checkCallback = function (isLogin) {
if (isLogin) {
console.log("fine");
res.render("index");
} else {
console.log("get lost");
res.render("login");
}
}
check([username, password], checkCallback);
});
// -----------------------------
// login.js
var check = function(arr, callback) {
var con = require(__dirname + "/database.js");
var login_check = false;
con.connect(function(err) {
if (err) throw err;
con.query(
"SELECT * FROM users WHERE (name = ?) AND (password = ?)",
[arr[0], arr[1]],
function(err, result, fields) {
if (err) throw err;
if (result) {
login_check = true;
callback(true); // Call the callback function here after checking is done
}
}
);
});
return login_check;
};
module.exports = check;

Add every item of an Array to mysql db

I'm pretty new to Java Script and tried to write a small programm to add users to a database. My problem is, that my programs doesn't add every user ones. It adds the last user as often as there are users in the list. users has more than 2 Objects and all the Objects have all field filled.
for(i = 0; i<users.length; i++)
{
var user = users[i];
console.log(user.lastonline)
pool.getConnection(function(err, connection) {
if (err) throw err;
var quer = connection.query('INSERT INTO users SET `steamid` = '+ connection.escape(user.steamid)+', `name`='+connection.escape(user.name)+', `lastonline`='+connection.escape(user.lastonline)+' ON DUPLICATE KEY UPDATE `name`='+connection.escape(user.name)+', `lastonline`='+connection.escape(user.lastonline)+'', function(err, result) {
connection.release();
});
console.log(quer.sql);
});
}
I tried to rewrite this in a lot of different way, but most time I get something like this:
TypeError: Cannot read property 'steamid' of undefined
for(i = 0; i<users.length; i++)
{
pool.getConnection(function(err, connection) {
console.log(users[i]["steamid"]);
if (err) throw err;
var quer = connection.query('INSERT INTO users SET `steamid` = '+ connection.escape(users[i]["steamid"])+', `name`='+connection.escape(users[i].name)+', `lastonline`='+connection.escape(users[i].lastonline)+' ON DUPLICATE KEY UPDATE `name`='+connection.escape(users[i].name)+', `lastonline`='+connection.escape(users[i].lastonline)+'', function(err, result) {
connection.release();
});
console.log(quer.sql);
});
}
EDIT:
Rest of the programm
var mysql = require('mysql');
var Promise = require("bluebird");
var pool = mysql.createPool({
connectionLimit : 10,
host : 'localhost',
user : 'zar',
password : 'qxLLPa06iEs2Bzsu',
database : 'zar',
socketPath: '/var/run/mysqld/mysqld.sock'
});
pool.on('connection', function (connection) {
console.log("connection made")
});
//my testing users
var users = [];
times = Date.now();
user1 = {steamid:012345678912345658,name:"user1",lastonline:times};
user2 = {steamid:012345678912345628,name:"user2",lastonline:times};
user3 = {steamid:012345678912345618,name:"user3",lastonline:times};
users.push(user1);
users.push(user2);
users.push(user3);
Edit: Fixed to use only one connection.
Previous version was getting a new connection for every user.
You should use Promises:
pool.getConnection((err, connection) => {
if (err) {
console.log(err);
return;
}
var tasks = users.map((user) => {
return new Promise((resolve, reject) => {
if (err) {
return reject(err);
}
var quer = connection.query('INSERT INTO users SET `steamid` = ' + connection.escape(user.steamid) + ', `name`=' + connection.escape(user.name) + ', `lastonline`=' + connection.escape(user.lastonline) + ' ON DUPLICATE KEY UPDATE `name`=' + connection.escape(users.name) + ', `lastonline`=' + connection.escape(users.lastonline) + '', function (err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
});
Promise.all(tasks)
.then((results) => {
// Array of results passed in resolve
connection.release();
})
.catch((err) => {
// All errors you reject are catched here
});
});
This should workd but still, you are executing all queries in parallel, which can be pretty aggressive for the DB.
I suggest you to look into bluebird Promise.each for better results.

better way of selecting 1 to many?

I have an express.js based rest application. Please have a look on following code and suggest me what would be better way.
I want to select user and its associated images (1 user has many images).
function getUser (connection, req, res) {
var userId = req.params.id;
connection.query('SELECT * FROM user p'
+ ' WHERE p.id = ' + connection.escape(userId), function handleSql(err, rows) {
if (err){ logAndRespond(err,res); return; }
if (rows.length === 0){ res.send(204); return; }
var adId = rows[0].adId;
// load images
connection.query('SELECT id, url FROM image WHERE ad_id = ' + connection.escape(adId), function (err, imgRows) {
if (err){ logAndRespond(err,res); return; }
if (rows.length != 0){
rows[0].images = imgRows;
}
res.json({'user': rows});
connection.release();
});
});
}
You don't have to escape parameters by yourself
You don't release the connection if an error occurred
The problem now is I don't know what you want to do with selected rows. You are also checking the rows.length twice but if there weren't any records in the first query then the second one will not be executed.
function getUser(conn, req, res) {
conn.query("SELECT * FROM user p WHERE p.id = ?;", [req.params.id], function(err, rows) {
if (err) {
return logAndRespond(err, res);
}
if (!rows.length) {
return res.send(204);
}
conn.query("SELECT id, url FROM image WHERE ad_id = ?;", [rows[0].adId], function(err, imgRows) {
if (err) {
return logAndRespond(err, res);
}
if (rows.length) { // ???
rows[0].images = imgRows;
}
res.json({"user": rows});
conn.release();
});
});
}