NodeJS get the mysql query result - mysql

I have a table containing an ISBN number and a the available number of books. I want to make a query to to ISBN number and get the response about the number of the books with this ID, but I don't know how to write the proper function to get the query result?
db.checkPeldanyszam( rentISBN, response, callback) => {
if (err) {
res.status(500).render('error', { message: `Insertion unsuccessful: ${err.message}` });
} else {
console.log(err);
next();
}
});
exports.checkPeldanyszam = (req,callback) => {
console.log(req);
const query = `SELECT Peldanyszam, IF(Peldanyszam>0, "Jo", "Hibas") as isOkay FROM konyv
WHERE ISBN=${req};`
pool.query(query,callback);
}
Thank you.

Related

Getting value only from Node.js MySQL result object

I am trying to get a list of distinct values from a MySQL Column and return these in a single JSON array.
I have this current code:
app.get('/experiences/', function(req, res) {
res.setHeader('Access-Control-Allow-Origin', http://localhost:3000');
connection.query('SELECT DISTINCT experience FROM questions', function(err, data) {
err ? res.send(err) : res.json(data);
});
});
I want the result to look like:
{experience: ["1-3","1-5","5+"]}
but it currently looks like:
[{"experience":"1-3"},{"experience":"1-5"},{"experience":"5+"}]
You can reformat the data object you get by looping on the array and recreate an object with the correct format.
Like this :
connection.query('SELECT DISTINCT experience FROM questions', function(err, data) {
if(err)
res.send(err)
else {
let experiences = [];
data.forEach(function(
experiences.push(d.experience);
}
result = {experience : experiences };
res.json(result);
}
});

Undefined push to array - Basic Application using ExpressJs and MySQL

First of all, I have to tell you I'm pretty noob in this "universe". I'm using: ExpressJs, MySql, Body-Parser, Express-session, Ejs template for creating an Basic Contacts Application in Node.
My database is composed from 3 tables:
user (user_id, first, second name, username, password)
contacts (ct_id, first, second name, phone numb.)
user_contacts (user_id, ct_id) --> foreign keys for user and contacts
I want to listing on /myProfile page all details about user and his contacts.
I don't know how to handle the select queries.
So, after some documentation I did this:
conn.query('SELECT * FROM user_contacts WHERE user_id= ?', req.session.user_id, function (err, result) {
if(err) throw err;
console.log(result);
var queryArray = "";
for(var i = 0; i < result.length; i++){
queryArray += `SELECT * FROM contacts WHERE ct_id= ${result[i].ct_id}; `;
}
console.log(queryArray);
conn.query(queryArray, function (err, result) {
if(err) throw err;
console.log(result);
res.render('myProfile/contacts', {
title: `${req.session.user_nickname}'s Contacts`,
data: result
});
});
});
But I have an error
ER_PARSE_ERROR: You have an error in your SQL syntax;
..when queryArray.length > 1
I searched and it's something about Multiple statement queries but I dont know how to solve it.
Edit 2:
I modify my code..
conn.query('SELECT * FROM user_contacts WHERE user_id= ?', req.session.user_id, function (err, result) {
if(err) throw err;
var datas = [];
for(var i = 0; i < result.length; i++){
getContacts = function(query){
conn.query(query, function (err, result) {
console.log('Creating data');
data = {
user: req.session.user_nickname,
contact:{
ct_firstName: result[0].ct_firstName,
ct_SecondName: result[0].ct_SecondName,
ct_PhoneNumber: result[0].ct_PhoneNumber
}
}
return data;
});
}
console.log('Send data to array');
datas.push(getContacts(`SELECT * FROM contacts WHERE ct_id = ${result[i].ct_id}`));
}
console.log(datas); // [ undefined, undefined ]
res.render('myProfile/contacts',{
title: `${req.session.user_nickname}'s profile`,
data: datas
})
});
But now my array contain undefined objects?? Any solution?
Maybe is something about scope?
My result:
Send data to array
Send data to array
[ undefined, undefined ]
Creating data
Creating data
I push the object to array before creating it. How is it possible?
1797,
I noticed you have several small queries grabbing the contact info for a given user. You could simplify your code by combining your queries into a single one. Often times 1 big query is more efficient (plus it's easier to maintain). I'm using a join. More info here.
const contacts = [];
const query = "
SELECT c.*
FROM user_contact uc
JOIN contact c ON uc.contact_id = c.contact_id
WHERE uc.user_id = ?
GROUP BY c.contact_id
";
conn.query(query, req.session.user_id, (err, results) => {
if (err) throw new Error(err);
// it seems that this could just be 'contacts = results' since they
// have the same structure
contacts = results.map(result => {
return {
ct_firstName: result[0].ct_firstName,
ct_SecondName: result[0].ct_SecondName,
ct_PhoneNumber: result[0].ct_PhoneNumber
};
});
res.render('myProfile/contacts',{
title: `${req.session.user_nickname}'s profile`,
data: contacts
});
});

nodejs mysql queries showing only one records instead of all records in the database

Am trying to retrieve all the database records from a table called post using node js but the problem is that only one record is retrieved instead of all.
In php I can use while() loop to loop through the database record to get all data.
Currently, I do not know how to neatly loop through the database in nodejs to get all the records from database. Some Stackoverflow scholars suggest using await/async method but i do not know to to implement it on the code below to make it work. can someone help me fix the issue.
var connection = require('./config');
module.exports.getpost = function (req, res) {
connection.query('SELECT * FROM posts', function (error, results, fields) {
if (error) {
console.log('error');
res.json({
status : false,
message : 'there are some error with the query'
});
} else {
var postid = results[0].id;
var title = results[0].title;
var content = results[0].content;
var type = -1;
console.log(title);
// Checking user status
connection.query('SELECT count(*) as cntStatus,type FROM like_table WHERE userid= ? and postid=?', [userid,postid], function (error, results, fields) {
if (error) {
console.log('error');
res.json({
status : false,
message : 'there are some error with the query'
});
} else {
var total_count = results[0].cntStatus;
if(total_count > 0){
type = results[0].type;
}
var total_count = results[0].cntStatus;
var result = {
"id" : postid,
"title" : title,
"content" : content,
"type" : type,
"likes" : total_count
};
console.log('query okay');
res.json({
//data:results,
data : result
});
}
});
}
});
}
I'm assuming you're using mysql npm. In that case I'm not sure what is the problem in your case. Results param is an array of rows returned by your select statement. So you can use loop to iterate trough all the rows.
You don't actually need to use async/await (which doesn't have any advantage in terms of functionality but looks cleaner). But if you want to get rid of callbacks you need to wrap connection query into a promise or use mysql2 npm which has promise interface. Here is how you can iterate trough all the rows from your select using async/await instead of callback:
var connection = require('./config');
module.exports.getpost = async function (req, res) {
try {
const queryResult = await query('SELECT * FROM posts');
queryResult.forEach(row => {
console.log(row.title);
})
} catch (err) {
console.log('error');
res.json({
status: false,
message: 'there are some error with the query'
});
}
}
Please note that you need to use nodejs 8 to run the code with async/await.
Also you don't need to do another query inside of your posts query, you can merge those two using SQL join
async waterfall - Runs an array of functions in series, each passing their results to the next in the array. However, if any of the functions pass an error to the callback, the next function is not executed and the main callback is immediately called with the error.
var connection = require('./config');
var async = require('async');
module.exports.getpost = function (req, res) {
var arrayOfFuncs = [];
var func_1 = function(callback) {
connection.query('SELECT * FROM posts', function (error, results, fields) {
if (error) {
console.log('error');
callback(error, null);
} else {
var toPass = {};
toPass.postid = results[0].id;
toPass.title = results[0].title;
toPass.content = results[0].content;
toPass.type = -1;
callback(null, toPass);
}
})
}
arrayOfFuncs.push(func_1);
var func_2 = function(prevData, callback) {
connection.query('SELECT count(*) as cntStatus,type FROM like_table WHERE userid= ? and postid=?', [userid,prevData.postid], function (error, results, fields) {
if (error) {
console.log('error');
callback(error, null);
} else {
var total_count = results[0].cntStatus;
if(total_count > 0){
type = results[0].type;
}
var total_count = results[0].cntStatus;
var result = {
"id" : postid,
"title" : title,
"content" : content,
"type" : type,
"likes" : total_count
};
console.log('query okay');
callback(null, result);
}
});
}
arrayOfFuncs.push(func_2);
async.waterfall(arrayOfFuncs, function(errString, finalResult) {
if(errString) {
return res.send(errString);
} else {
return res.send(finalResult);
}
});
}

Node MySQL Populate Children as an array

I'm new to MySQL and coming from a Mongo background, so I'm used to Mongoose's .populate() method for retrieving a document's associations as a sub-array. Is there a way to do that in MySQL? Right now I'm trying to retrieve a single quiz with all of its questions in a single query. I have a quizzes table and a questions table, which has a parent_quiz_id field that refers to a quiz id. I've tried several permutations of SELECT * FROM quizzes JOIN questions ON questions.parent_quiz_id = quizzes.quiz_id WHERE quizzes.quiz_id = ? but they give me all the quiz information for each question, and that seems inefficient.
Is there a way for a single query to retrieve the quiz information once, and in subsequent rows populate all the questions that belong to that quiz?
This is what I'm doing now, with two separate queries:
connection.beginTransaction(function(err) {
if (err) { throw err; }
//First get the quiz data
connection.execute('SELECT * FROM quizzes WHERE quiz_id = ? LIMIT 1', [id], function (err, quiz_results) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
const quiz = quiz_results[0];
if(!quiz){
return done({ message: "No quiz found with that id"}, false);
}
//Now get all of the quiz's questions
connection.execute('SELECT * FROM questions WHERE parent_quiz_id = ?', [id], function (err, question_results) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
quiz['questions'] = question_results;
return done(null, quiz);
});
});
});
});
But I'd rather not use two separate queries. For references, I'm using Node MySQL2

How can I get just the Count result?

I have this node msnodesqlv8 connection and I am counting the number of records from one table. I am getting the result as { total: 26 } and the expected result should be 26. Here is my code:
pool.connect().then(() => {
pool.request().query('SELECT count([TaskID]) as total FROM [db_test].[dbo].[tb_test]', (err, result) => {
myResults = result.recordset[0];
console.log(myResults);
})
});
As you are getting data in object form. now you have to access the object from the result.
pool.connect().then(() => {
pool.request().query('SELECT count([TaskID]) as total FROM [db_test].[dbo].[tb_test]', (err, result) => {
myResults = result.recordset[0].total;
console.log(myResults.total); // it should provides you expected result
})
});