Node.js sequential mysql queries promise not resolving - mysql

A route in app.js calls a function register(user) in a MySQL model, model.js. This register() calls displayNameTaken(display_name) which will return null if display name is available otherwise it will return a json object.
The promise in the app.post containing model.register(req.body) does not resolve.
If display name is taken register() will pass this json object back to the calling route.
If display name is not taken register() will register user and return back another json object back to the calling route.
The app never resolves the returned promise, app#113.
Or do you have any suggestions to what I should do instead?
Can you see what I have done wrong?
Output below:
1. When display name taken
app#113 [ undefined ]
model#73 { code: 12, message: 'e' }
2. Display name not taken, registration successful
app#113 [ undefined ]
model#73 undefined
model#61 110 //<- last insert id
The app never resolves the returned promise, app#113.
Or do you have any suggestions to what I should do instead?
Can you see what I have done wrong?
app.post('/api/register', function (req, res) {
Promise.all([
model.register(req.body)
]).then((r => {
console.log('app#113',r);// r=> undefined
res.status(200).json(r);
})).catch((e => {console.log(e);
res.status(500).json(e);
}));
});
function Model(db){
this.db = db;
}
//Function returns null if display name is not taken
Model.prototype.displayNameTaken = function(display_name){
return new Promise((resolve, reject, next) => {
var sql = "SELECT id FROM `users` WHERE `display_name` = ?";
var rv;
this.db.query(sql, [[display_name]], (err, result) => {
if (err) {
return resolve(err);
}
if(0 < result.length && result[0].id != undefined && result[0].id != NaN && 0 < result[0].id){
rv = {code: 12, message:'e'};
}else{
rv = null;
}
return resolve(rv);
});
});//Promise
}
model.register = function register(params){
if(params == undefined){
return;
}
var rv;
Promise.all([
this.displayNameTaken(params.display_name.trim())
]).then((r => {
return new Promise((resolve, reject, next) => {
if(r[0] == null){//display_name available
var sql = "INSERT INTO `users` (`display_name`, `email`, `hash`, `created`,`md51`, `md52`, `language`) VALUES ?";
var md51 = md5(randomString({length:32}));
var md52 = md5(randomString({length:32}));
var user = [[
params.display_name.trim(),
params.email.trim(),
passwordHash.generate(params.hash.trim()),
datetime.create().format('Y-m-d H:M:S'),
md51,
md52,
params.language
]];
this.db.query(sql, [user], function (err, result) {
if (err) {
return reject(err);
}
console.log('model#61',result.insertId);
if(0 < result.insertId){
rv = {code: 8, message:'i', md51: md51, md52: md52};
}else{
rv = {code: 0, message:'e'};
}
return resolve(rv);
});
}else{//display_name taken
rv = r[0];
}
console.log('model#73',rv);
return resolve(rv);
});//Promise
})).catch((e => {
console.log(e);
}));

Related

Is there a way to pass a value from a mysql callback function to the outer function in express?

I'm using express and npm MySQL to develop an API.I have a json request in this format:
{
"payments":[
{
"PolicyNo": "ME3",
"PaymentDate": "2019-04-16T18:00:00.000Z",
},
{
"PolicyNo": "PIN001q",
"PaymentDate": "2019-04-16T18:00:00.000Z",
}]
}
I want to check the database if the policyNo exists before inserting. To avoid the common ERR_HTTP_HEADERS_SENT, I've looped through the payments querying the database with the PolicyNo. If it exists it's pushed into a success array if it doesn't it's pushed into a failed array.
This works perfectly but I can't access these arrays outside the callback.
Here's what I've tried:
router.post('/bla', (req, res)=>{
const values = []
const failedvalues = []
let sql = 'SELECT PolicyNo from pinclientinfo WHERE PolicyNo=?'
req.body.payments.forEach(element => {
connection.query(sql,element.PolicyNo,(err, rows) =>{
if(!err){
if(rows && rows.length > 0){
values.push(element.PolicyNo, element.PaymentDate)
}else{
failedvalues.push(element.PolicyNo)
}
}
})
})
res.json({
failed:failedvalues,
success:values
})
})
Here's the response I'm getting:
{
"failed": [],
"success": []
}
This has some major problems, mostly conceptually.
Firstly the forEach is synchronous will be called payments.length number of times, but the sql query is Asynchronous so it will complete in the future.
I think you are confused between synchronous and asynchronous functions and how they work.
But you can solve this (in your case) atleast two ways.
1) Use the IN syntax and get the array. Iterate over it and do stuff. "SELECT PolicyNo from pinclientinfo WHERE PolicyNo in (...)"
let sql = 'SELECT PolicyNo from pinclientinfo WHERE PolicyNo IN (' + Array(req.body.payments).fill('?').join(',') + ')'
const policies = req.body.payments.map(p => p.PolicyNo);
const values = [];
const failedvalues = [];
connection.query(sql, ...policies, (err, rows) => {
//now check each row..
rows.forEach(element => {
//Not optimized only depicts logic
///do stuff
/// like fill values and failedvalues
if(policies.indexOf(element.PolicyNo) > -1){
values.push(...)
}else{
failedvalues.push(...)
}
});
res.json({
failed: failedvalues,
success: values
})
})
Which will be 1 DB call.
2) The other approach is (not very good) doing multiple db calls and check for count.
let sql = 'SELECT PolicyNo from pinclientinfo WHERE PolicyNo=?'
let count = 0;
req.body.payments.forEach(element => {
connection.query(sql, element.PolicyNo, (err, rows) => {
if (!err) {
if (rows && rows.length > 0) {
values.push(element.PolicyNo, element.PaymentDate)
} else {
failedvalues.push(element.PolicyNo)
}
}
// check If all Complete
count+=1;
if(count === req.body.payments){
//all complete
res.json({
failed: failedvalues,
success: values
})
}
})
})
BUT SERIOUSLY, USE PROMISE. USE ASYNC/AWAIT USE THOSE SWEET LITTLE FEATURES ES6 GIVES YOU
Check out: this post
because connection.query is asynchronous, so return:
{
"failed": [],
"success": []
}
use promise and await you can synchronized resolve mysql data
use Promise.all() you can synchronized resolve list of promise
router.post("/bla", async (req, res) => {
let values = [];
let failedvalues;
let promises = [];
let sql = "SELECT PolicyNo from pinclientinfo WHERE PolicyNo=?";
req.body.payments.forEach(element => {
promises.push(
new Promise(function(resolve, reject) {
connection.query(sql, element.PolicyNo, (err, rows) => {
if (!err) {
if (rows && rows.length > 0) {
values.push(element.PolicyNo, element.PaymentDate);
} else {
failedvalues.push(element.PolicyNo);
}
}
resolve();
});
})
);
});
await Promise.all(promises);
res.json({
failed: failedvalues,
success: values
});
});

Reading a JSON file in NodeJs

I have a small JSON file with this content
{
"users": [
{
"id": 1593,
"name": "Foo Bar"
}
]
}
and I want to read this content by using the filesystem module. So my application looks this
const fs = require('fs');
const express = require('express');
const app = express();
app.get('/users/:id', function (req, res) {
fs.readFile('./userDb.json', 'utf8', function (err, data) {
var json = JSON.parse(data);
var users = json.users;
console.log(users[0].id); // returns 1593
console.log(req.params.id); // returns 1593
var userObj = null;
for(var i = 0; i < users.length; i++){
var currentUser = users[i];
console.log(currentUser.id); // returns 1593
if (currentUser.id === req.params.id) { // this should be fine 1593 === 1593
userObj = currentUser;
break;
}
}
console.log(userObj); // returns undefined
res.render('users', {
user: userObj
});
});
});
app.listen(3000, function () {
console.log('Server running on port 3000');
});
The log will always return 1593, when I pass it as a parameter but when I want to render my handlebars template, the assigned object is null.
userObj is null, even when trying this code
var userObj = users.find(u => u.id === req.params.id);
but I think the database is not wrong. Where did I made a mistake =?
Pretty sure that req.params.id is a String. Try :
const userObj = users.find(u => u.id === Number(req.params.id));
When you have a doubt about it :
console.log(
`First value : ${v1} - ${typeof v1}`,
`Second value : ${v2} - ${typeof v2}`,
);
Please replace
if (currentUser.id === req.params.id)
with
if (currentUser.id == req.params.id)
Since req.params.id is a string and and currentUser.id is a number, they cannot be compared strictly .
However a non-strict comparison should work fine.
console.log(userObj); // returns undefined
is called after the readFile callback, this is an asynchronous non-blocking event!
You have to move your:
res.render('users', {
user: userObj
});
at the end of readFile function

promise returns undefined while calling from two different mysql queries

I Have two mysql queries that runs with promise.
The first one is updates information on a mysql table and then resolves the issue and calls the next mysql query. The problem is that, when it calls the next mysql query the promise returns UNDEFINED and I am not sure why. When I console.log it out in my node js server post request, it gives undefined. I documented on the code which areas are problems.
UpdateUserPath = (data) => new Promise((resolve,reject)=>{
data.UPDATE_DT = getDateTime();
db.query('UPDATE path UPDATE_DT = ? where Owner = ?',
[data.UPDATE_DT, data.Owner], function(err,results,fields){
if(err){
reject('Could not update user path');
}else{
if(results.affectedRows > 0){
data.ID = null;
data.UPDATE_DT = null;
// The problem is here, when this gets resolved it calls the other function SaveUserPath
resolve(saveUserPath(data));
}else{
reject('Could not update user path');
}
}
});
});
saveUserPath = (data) => new Promise((resolve, reject) => {
db.query('INSERT INTO path SET ?', data, function (error, results, fields) {
if (error) {
reject('Could not insert path');
}else{
var Id = results.insertId;
db.query('UPDATE path SET ORIG_ID = ? where ID = ?',[Id, Id], function(err,results,fields){
if(err){
reject('Could not insert row to path table - saveuserpath');
}else{
if(results.affectedRows > 0){
// THIS INFORMATION HERE IS UNDEFINED
return resolve(results[0]);
}else{
reject('Could not update path');
}
}
});
}
});
});
In the server it gets called like this.
getUserPath(req.session.userid).then((path_data)=>{
path_data.status = 1;
UpdateUserPath(path_data).then((result)=>{
console.log(result); // THIS IS UNDEFINED
});
});
I am wondering if resolve(saveUserPath(data)); is the right way to call another promise which is not outside in the server.
I was thinking of just doing it this way.
UpdateUserPath(path_data).then((result)=>{
saveUserPath(result).then((result_save) => {
console.log(result_save); // THIS MIGHT WORK
});
});
But why is the normal way wrong.
I have several guesses why it isn't working, but there are a number of things wrong such that it's better to just clean up the code to a much better design.
When combining multiple asynchronous callback-driven operations in an otherwise promise-based interface, you really want to promisify the underlying functions at their lowest level and then you can implement all your control flow and error handling using the benefits of promises. I think that will also make your problem go away and probably fix a couple other bugs too.
// promisify db.query()
// if a promisified interface is built into your database, use that one instead
db.queryP = function(q, d) {
return new Promise((resolve, reject) {
db.query(q, d, (err, results, fields) => {
if (err) {
reject(err);
} else {
resolve(results);
}
});
});
}
UpdateUserPath = function(data) {
data.UPDATE_DT = getDateTime();
let q = 'UPDATE path UPDATE_DT = ? where Owner = ?';
return db.queryP(q, [data.UPDATE_DT, data.Owner]).then(results => {
if (results.affectedRows > 0) {
data.ID = null;
data.UPDATE_DT = null;
return saveUserPath(data);
} else {
throw new Error('Could not update user path');
}
});
}
saveUserPath = function(data) {
let q = 'INSERT INTO path SET ?'
return db.queryP(q, data).then(results => {
let q2 = 'UPDATE path SET ORIG_ID = ? where ID = ?';
var Id = results.insertId;
return db.queryP(q2, [Id, Id]).then(results2 => {
if (results2.affectedRows > 0) {
return results2[0];
} else {
throw new Error('Could not update path');
}
});
});
}
getUserPath(req.session.userid).then(path_data => {
path_data.status = 1;
return UpdateUserPath(path_data);
}).then(result => {
// process result here
}).catch(err => {
// process error here
});

MySQL Node.js Syntax Error (DRIVING ME CRAZY)

I'm getting an error that says:
C:\Users\Minh Lu\Desktop\MusicMediaWebApp\database\dbService.js:34
[0] con.query(sql, typeCast: function(field, next) {
[0] ^^^^^^^^
[0]
[0] SyntaxError: missing ) after argument list
From this:
/* Retrieves a User model by ID */
getUserByID: function(ID, callback) {
this.tryConnect().getConnection(function(err, con) {
var sql = queries.getUserByID;
con.query(sql, typeCast: function(field, next) {
// We only want to cast bit fields that have a single-bit in them. If the field
// has more than one bit, then we cannot assume it is supposed to be a Boolean.
if ( ( field.type === "BIT" ) && ( field.length === 1 ) ) {
var bytes = field.buffer();
// A Buffer in Node represents a collection of 8-bit unsigned integers.
// Therefore, our single "bit field" comes back as the bits '0000 0001',
// which is equivalent to the number 1.
return( bytes[ 0 ] === 1 );
}
return next();
}, ID, function (err, result) {
if (err) throw err;
// Call the callback function in the caller of this method so we can do something with this "result"
return callback(result); // [] if not found
});
});
},
And I'm so confused as to what syntax error this is? This is the same method as in the documentation: https://github.com/mysqljs/mysql#type-casting
Thanks!
Look closely at the documentation.
connection.query({
sql: '...',
typeCast: function (field, next) {
if (field.type == 'TINY' && field.length == 1) {
return (field.string() == '1'); // 1 = true, 0 = false
}
return next();
}
});
connection.query(...) is accepting an object as the parameter.

How to execute two mysql queries using promise in nodejs?

Here is what i going to achieve, i want to have an JSON data that returned from my node.js server is joined based on the value of first mysql queries (array JSON data)
if i just want to execute two mysql queries i just enable multipleStatements: true then the code will be like this :
app.post('/product', function (req, res) {
connection.query('call getProductList; call rowCountTotal', function (err, rows, fields) {
if (!err) {
var response = [];
if (rows.length != 0) {
response.push({ 'result': 'success', 'data': rows });
} else {
response.push({ 'result': 'error', 'msg': 'No Results Found' });
}
res.setHeader('Content-Type', 'application/json');
res.status(200).send(JSON.stringify(response));
} else {
res.status(400).send(err);
}
});
});
than the data will showed up two JSON that are separated in two arrays, but what i want to build here is one JSON with multiple array JSON data, which is looked like this :
Sample JSON that i want :
[
{
"product_id":"1",
"product_name":"MX-001",
"product_attachment":[
{
"product_id":"1",
"product_attachment_id":"1",
"file_name":"maxgrand5.jpg",
"file_path":"assets"
}
]
}
]
And here is what i trying to do in my node.js server side code, i trying to use
Promise.all (i think this code i should use right?) :
return new Promise(function(resolve, reject) {
Promise.all(connection.query('call getProductSingle("'+ product_series +'")', function (err, rows, fields) {
if (!err) {
var response = [];
if (rows.length != 0) {
response.push({ 'result': 'success', 'data': rows });
} else {
response.push({ 'result': 'error', 'msg': 'No Results Found' });
}
connection.query('call getProductAttachment("'+ rows[0][0].product_id +'")', function (err, rowsAttachment, fields) {
if (!err) {
console.log("second query");
if (rowsAttachment.length != 0) {
response.push({'product_attachment': rowsAttachment });
} else {
response.push({ 'result': 'error', 'msg': 'No Results Found' });
}
}
});
console.log("outside second query");
res.setHeader('Content-Type', 'application/json');
res.status(200).send(JSON.stringify(response));
} else {
res.status(400).send(err);
}
console.log("last");
if (err) {
return reject(err);
}
resolve(res);
}));
});
here is my Stored Procedure result which named in 'getProductSingle' :
product_id = 1
product_name = MX-001
and here is my second procedure result 'getProductAttachment' :
product_id = 1
file_name = maxgrand5.jpg
file_path = assets
product_attachment_id = 1
one single product_id can have more than 1 product_attachment_id
how can i get the data joined?
I just updated my question, the problem is the second query is too late when i make the request, i should use promise to make it not late, how to do this?
First I created the query, in which the product_single table is joined to the product_attachments, maybe you want to restrict it with an WHERE clause or a paging mechanism with LIMIT and OFFSET:
SELECT ps.product_id, ps.product_name, pa.product_attachment_id,
pa.file_name, pa.file_path
FROM product_single ps
LEFT JOIN product_attachment pa
ON ps.product_id = pa.product_id
ORDER by ps.product_id,pa.product_attachment_id;
In the following code I will refer to this query with a call product_join_att.
return new Promise(function(resolve, reject) {
var result_products = [];
var result_attachments = [];
var old_id = undefined;
var old_name = undefined;
var new_id = undefined;
var row_index = 0;
connection.query('call product_join_att', function (err, rows, fields) {
if (err) {
reject(err);
return;
} else if (rows.length == 0) {
reject(new Error('No Results found'));
return;
}
while (row_index < rows.length ) {
new_id = rows[row_index].product_id;
if (old_id !== new_id) { // any new line with an new id...
if (typeof old_id !== 'undefined') { // when the old line is existing
result_products.push( // push the product
{"product_id": old_id.toString(),
"product_name":old_name,
"product_attachment": result_attachments
});
}
old_id = new_id; // remember the new_id
old_name = rows[row_index].product_name;// and name
product_attachments = []; // and initialize attachments.
}
product_attachments.push({ // provide the inner attachment informations.
"product_id": new_id,
"product_attachment_id" : rows[row_index].product_attachment_id,
"file_name" : rows[row_index].file_name;
"file_path" : rows[row_index].file_path;
});
row_index++; // and go to the next row.
}
if (typeof old_id !== 'undefined') { // if there are still data
result_products.push( // push the last line also.
{"product_id": old_id.toString(),
"product_name":old_name,
"product_attachment": result_attachments
});
}
} // query
resolve(result_products);
} // end of promise...
i figure it out with simplier solution but this way is not a "promise" way to get it done, so decide it which to use if you guys face the problem like this. Since i dont need many data to loop, just a single root array JSON with one dimension JSON array, i will just put it this way :
app.post('/getsingleproduct', function (req, res) {
var product_series = req.body.product_series;
connection.query('call getProductSingle("'+ product_series +'")', function (err, rows, fields) {
if (!err) {
var response = [];
if (rows.length != 0) {
response.push({ 'result': 'success', 'data': rows[0] });
} else {
response.push({ 'result': 'error', 'msg': 'No Results Found' });
}
connection.query('call getProductAttachment("'+ rows[0][0].product_id +'")', function (err, rowsAttachment, fields) {
if (!err) {
if (rowsAttachment.length != 0) {
response[0].data[0]['product_attachment']= rowsAttachment[0];
res.setHeader('Content-Type', 'application/json');
res.status(200).send(JSON.stringify(response));
} else {
response.push({ 'result': 'error', 'msg': 'No Results Found' });
res.status(400).send(err);
}
}
});
} else {
res.status(400).send(err);
}
});
});
this is a non-promise way, if you need promise way, look for #Myonara answer, i think that the best