Node.js unable to run mysql query inside loop - javascript

I have two tables in mysql and want to query a table depending on the result of another, so I wrote a function like
export function getLocations(req, res) {
const database = new Database();
database.query('select * from districts')
.then(rows => {
let appData = [];
rows.forEach(row => {
const new_database = new Database();
new_database.query(`SELECT locations.id,locations.name, IF(subscriptions.id IS NULL,0,1) as subscribed
FROM locations
LEFT JOIN subscriptions
ON (subscriptions.location_id = locations.id AND subscriptions.user_id=1)
WHERE locations.district=?`,row.id)
.then(sub_rows => {
let district=row;
district["locations"]=sub_rows;
appData.push(district);
new_database.close();
}, err => {
return new_database.close().then(() => { throw err; })
})
.catch(err => {
console.log(err);
res.status(500).json("Database Error");
})
});
res.status(200).json(appData); //final result here
database.close()
}, err => {
return database.close().then(() => { throw err; })
})
.catch(err => {
console.log(err);
res.status(500).json("Database Error");
})
}
Here I want to get run second query based for each of the row of first query.
I am getting an empty array as result. My first query is executing properly and I logged to see all rows are being returned. What could be the issue?

You can make it work by making this async
rows.forEach(async row => {
const new_database = new Database();
await new_database.query(`SELECT locations.id,locations.name, IF(subscriptions.id IS NULL,0,1) as subscribed
FROM locations
LEFT JOIN subscriptions
ON (subscriptions.location_id = locations.id AND subscriptions.user_id=1)
WHERE locations.district=?`,row.id)
.then(sub_rows => {
let district=row;
district["locations"]=sub_rows;
appData.push(district);
new_database.close();
}, err => {
return new_database.close().then(() => { throw err; })
})
.catch(err => {
console.log(err);
res.status(500).json("Database Error");
})
});
The operation you are performing is I/O and JS is single threaded. It means in layman terms it will not wait will iterating your loop where it is going to be making a request where there is some wait while the request processes. You need to tell JS that this event is asynchronous. For this you need to use async/await
Guides
forEach async/await
async/await MDN documentation

I don't have the environment in this machine. it may have some errors but you can fix it if it have, take a look at the following logic
export function getLocations(req, res) {
const database = new Database();
const promises=[];
database.query('select * from districts')
.then(rows => {
let appData = [];
rows.forEach(row => {
promises.push(getAnotherQuery(row));
});
database.close()
}, err => {
return database.close().then(() => { throw err; })
})
.catch(err => {
console.log(err);
res.status(500).json("Database Error");
})
return Promise.all(promises).then(result)=> res.status(200).json(result); //final result here
}
getAnotherQuery=(row)=>{
return new Promise((resolve,reject)=>{
const new_database = new Database();
const appData=[]
new_database.query(`SELECT locations.id,locations.name, IF(subscriptions.id IS NULL,0,1) as subscribed
FROM locations
LEFT JOIN subscriptions
ON (subscriptions.location_id = locations.id AND subscriptions.user_id=1)
WHERE locations.district=?`,row.id)
.then(sub_rows => {
let district=row;
district["locations"]=sub_rows;
appData.push(district);
new_database.close();
resolve(appData);
}, err => {
return new_database.close().then(() => { throw err; })
})
.catch(err => {
console.log(err);
res.status(500).json("Database Error");
})
});
}

Related

Can't send request in componentDidMount React Native

I have an issue with sending a request to backend from my componentDidMount(). Basically I need to do two things before rendering screen:
Obtain data from API call and save it to state
Send that obtained data to backend and take response values from backend.
The problem I've faced on first step is that setState() is async, and even though my array is not empty (I see it's elements in render() and componentDidUpdate fucntion) in componentDidMount() when I console.log() array it will be empty. Now, the issue is: I still need to send that state array to backend before showing the screen. But how can I do it, when it appears empty there?
I have everything working fine if I send the request from the Button element in my render function, but that's not exactly what I need. Any suggestions?
this.state = {
ActivityItem: [],
}
componentDidMount() {
this.getDataFromKit(INTERVAL); //get data from library that does API calls
this.sendDataToServer(); //sending to backend
}
componentDidUpdate() {
console.log("componentDidUpdate ", this.state.ActivityItem) // here array is not empty
}
getDataFromKit(dateFrom) {
new Promise((resolve) => {
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) {
return resolve([]);
}
const newData = results.map(item => {
return { ...item, name: "ItemAmount" };
});
this.setState({ ActivityItem: [...this.state.ActivityItem, ...newData] })
})
});
And last one:
sendDataToServer() {
UserService.sendActivityData(this.state.ActivityItem).then(response => {
}).catch(error => {
console.log(error.response);
})
And here it works as expected:
<Button
title='send data!'
onPress={() => this.sendDataToServer()
} />
UPDATE
If I have like this (wrapped inside initKit function this will return undefined.
AppleKit.initKit(KitPermissions.uploadBasicKitData(), (err, results) => {
if (err) {
return;
}
return new Promise((resolve) => {
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) return resolve([]);//rest is the same
you have to wait for the promise to resolve. You need something like this:
componentDidMount() {
this.getDataFromKit(INTERVAL).then(result => {
this.sendDataToServer(result); //sending to backend
}).catch(e => console.error);
}
and you can update your other function that fetches data to return it:
getDataFromKit(dateFrom) {
return new Promise((resolve) => {
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) return resolve([]);
const newData = results.map(item => {
return { ...item, name: "ItemAmount" };
});
const allData = [ ...this.state.ActivityItem, ...newData ];
this.setState({ ActivityItem: allData });
resolve(allData);
});
});
}
finally, you need the 'sendData' function to not depend on state, but get a param passed to it instead:
sendDataToServer(data) {
UserService.sendActivityData(data).then(response => {
// ... do response stuff
}).catch(error => {
console.log(error.response);
});
}
Handling Multiple Requests
if the requests don't depend on each other:
componentDidMount() {
Promise.all([
promise1,
promise2,
promise3,
]).then(([ response1, response2, response3 ]) => {
// do stuff with your data
}).catch(e => console.error);
}
if the requests do depend on each other:
componentDidMount() {
let response1;
let response2;
let response3;
promise1().then(r => {
response1 = r;
return promise2(response1);
}).then(r => {
response2 = r;
return promise3(response2);
}).then(r => {
response3 = r;
// do stuff with response1, response2, and response3
}).catch(e => console.error);
}
as far as your update, it seems like you wrapped your async request in another async request. I'd just chain it instead of wrapping it:
make the initKit a function that returns a promise
function initKit() {
return new Promise((resolve, reject) => {
AppleKit.initKit(
KitPermissions.uploadBasicKitData(),
(err, results) => {
if (err) reject({ error: 'InitKit failed' });
else resolve({ data: results });
}
);
});
}
make get samples a separate function that returns a promise
function getSamples() {
return new Promise((resolve) => {
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) resolve([]); //rest is the same
else resolve({ data: results });
});
});
}
chain 2 promises back to back: if initKit fails, it will go in the .catch block and getSamples wont run
componentDidMount() {
initKit().then(kit => {
return getSamples();
}).then(samples => {
// do stuff with samples
}).catch(e => console.log);
}

Delete docs from Firebase

I have an array that contains the documents id of the firebase. I need to click on the button to delete these documents in the firebase.
deletePosts() {
db.collection("users")
.doc(user.email)
.collection("posts")
.doc(this.selectedPosts[0].id)
.delete()
.then(() => {
console.log("Success!");
})
.catch(err => {
console.log(err);
});
}
},
How can I iterate documents and delete them?
You could use a batched write as follows:
deletePosts() {
let batch = db.batch();
this.selectedPosts[0].forEach(element => {
batch.delete(db.collection("users").doc(user.email).collection("posts").doc(element.id));
});
batch.commit()
.then(() => {
console.log("Success!");
})
.catch(err => {
console.log(err);
});
}
Note that a batched write can contain up to 500 operations. In case you foresee that you could have to delete more than 500 you could use Promise.all(), as follows:
deletePosts() {
const promises = [];
this.selectedPosts[0].forEach(element => {
promises.push(db.collection("users").doc(user.email).collection("posts").doc(element.id).delete());
});
Promise.all(promises);
.then(() => {
console.log("Success!");
})
.catch(err => {
console.log(err);
});
}

Capturing errors with Async/Await

I have a part of my code that makes several API calls to different endpoints and I want to know if any of those calls fail so I can display an appropriate error message. Right now, if an error happens in one() it will stop all other calls from happening, but that's not what I want; If an error occurs, I want it to populate the errors object and have the program continue on.
async function gatherData() {
let errors = { one: null, two: null, three: null };
const responseOne = await one(errors);
const responseTwo = await two(errors);
const responseThree = await three(errors);
if (!_.isNil(errors.one) || !_.isNil(errors.two) || !_.isNil(errors.three)) {
// an error exists, do something with it
} else {
// data is good, do things with responses
}
}
gatherData();
async function one(errors) {
await axios
.get("https://jsonplaceholder.typicode.com/comment")
.then(res => {
return res;
})
.catch(err => {
errors.one = err;
return err;
});
}
async function two(errors) {
await axios
.get("https://jsonplaceholder.typicode.com/comments")
.then(res => {
return res;
})
.catch(err => {
errors.two = err;
return err;
});
}
async function three(errors) {
await axios
.get("https://jsonplaceholder.typicode.com/comments")
.then(res => {
return res;
})
.catch(err => {
errors.three = err;
return err;
});
}
If you pass the errors to the async functions, so pass the errors object as parameter
const responseOne = await one(errors);
const responseTwo = await two(errors);
const responseThree = await three(errors);

Update multiple SQL records with single query MySQL query with npm mysql

Thanks for taking a look at this! I am attempting to update a few records in MySQL with a single query; however, I am using a class with Promises to create a group of synchronis queries and I cannot seem to get this to work. Here is what I currently have:
req.body is an array of objects with two key/value pairs...
API:
router.post('/verified', (req, res) => {
let database = new Database(dbOptions);
let verifiedItemsArray = [];
let sqlQueryArray = [];
let updateQuery = 'UPDATE `raw_unverified` SET `funding_source` = ? WHERE `ritm_number` = ?';
let verifiedItemArray = [req.body[0].funding_source, req.body[0].ritm_number];
database.beginTransaction([updateQuery], verifiedItemArray)
.then(response => console.log(response))
.catch(error => console.log(error));
res.send('Update Successful');
});
database class:
class Database {
constructor(config) {
this.connection = mysql.createConnection(config);
}
query(sqlQuery, sqlArgs) {
console.log(sqlQuery);
return new Promise( (resolve, reject) => {
this.connection.query(sqlQuery, ((sqlArgs) ? [sqlArgs] : null), (error, results) => {
if (error) return reject(error);
resolve(results);
});
});
}
beginTransaction(sqlQueries, sqlArgs) {
return new Promise( (resolve, reject) => {
let allResults = [];
this.connection.beginTransaction( (error) => {
if (error) return reject(error);
for (let i = 0; i < sqlQueries.length; i++) {
this.query(sqlQueries[i], ((sqlArgs) ? sqlArgs[i] : null))
.then((results) => {
allResults.push(results);
})
.catch((error) => {
throw error;
});
}
this.connection.commit( (error) => {
if (error) return reject(error);
resolve(allResults);
});
});
});
}
}
module.exports = Database;
And here is the error that I get:
UPDATE raw_unverified SET funding_source = ? WHERE ritm_number = ?
(node:8216) UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
Any help will be much appreciated and anything that I could do better, please feel free to point out!
Thanks in advance everyone!
In this line
this.query(sqlQueries[i], ((sqlArgs) ? sqlArgs[i] : null))
you're passing arguments to queries by index - that means you need to change
database.beginTransaction([updateQuery], verifiedItemArray)
to
database.beginTransaction([updateQuery], [verifiedItemArray])

Create method chain before object exists

Let's say I'm using knex to run queries against an SQL database. I chain a few methods to build the query.
For example:
const sqlConfig = require('./sql.config');
var knex = require('knex')(sqlConfig);
knex.select("*")
.from("books")
.where("author", "=", "José Saramago")
.then((rows) => {
console.log(rows);
})
.catch((err) => {
console.log(err);
})
.finally(() => {
knex.destroy();
})
Now, my question is:
Is there a way to store the method chain before the knex object is created and call it later when it is created?
Something like this:
const methodChain = <<<
.select("*"),
.from("books"),
.where("author", "=", "José Saramago")
>>>
const sqlConfig = require('./sql.config');
var knex = require('knex')(sqlConfig);
knex
.methodChain()
.then((rows) => {
console.log(rows);
})
.catch((err) => {
console.log(err);
})
.finally(function() {
knex.destroy();
})
You could create a function that accepts the initial parameter in the chain:
function methodChain(in) {
return in.select("*")
.from("books")
.where("author", "=", "José Saramago");
}
methodChain(knex)
.then((rows) => {
console.log(rows);
})
.catch((err) => {
console.log(err);
})
.finally(function() {
knex.destroy();
})
Sure.
const methodChain = (x) => x
.select("*"),
.from("books"),
.where("author", "=", "José Saramago");
then later
methodChain(knex)
.then((rows) => {
console.log(rows);
})
.catch((err) => {
console.log(err);
})
.finally(function() {
knex.destroy();
})

Categories

Resources