JavaScript, Node.JS : unable to update a variable in a loop - javascript

I am building an API via Node.js.
I have a loop that add values to an initially empty array, but the array stays empty. The following is my code.
get_doc_name = async (event) => {
const connection = await db.connection()
try {
const doc_ids = event.doc_ids
// array that has JSONs as element
// e.g. [ {1 : 'certificate.pdf'}, {2: 'report.jpeg'}]
let dict = []
await doc_ids.map(async function(id) {
const sql = "SELECT link FROM document WHERE id=?"
await connection.query(sql, id, async function (err, result) {
if (err) throw err
const link = await result[0].link
const doc_name = await link.split('/').pop()
await dict.push({
key: id,
value: doc_name
})
console.log(dict)
})
})
return dict
} catch (err) {
console.log(err.message)
} finally {
connection.end()
console.log('MySQL connection closed')
}
}
The function returns an empty array. However, it prints the updated array when console.log(dict) is executed. I need the function to return the updated array.
Moreover, when the function is called, the codes in the 'finally' statement are executed before the codes in 'try' statement. The program prints MySQL connection closed before it prints dict.
Any help is highly appreciated!

As you are retrieving data from DB, it is better to use serial Promise instead of parallel Promises to get data from DB. The way I do is in serial Promise.
It is a better practice to get data with SELECT link FROM document WHERE id in () instead of id= in your SQL because you don't need to make multiple I/O to the DB in your case.
However, I didn't change this part of logic.
Also, you should promisefy your connection.query and return a value from it.
I make it in a getResult() function.
get_doc_name = async event => {
try {
const connection = await db.connection();
try {
const doc_ids = event.doc_ids;
// array that has JSONs as element
// e.g. [ {1 : 'certificate.pdf'}, {2: 'report.jpeg'}]
let dict = [];
//Promises in serial here
for (const id of doc_ids) {
const result = await getResult(connection, id);
dict.push(result);
}
console.log(dict);
return dict;
} catch (err) {
console.log(err);
} finally {
await connection.end();
console.log("MySQL connection closed");
}
} catch (err) {
console.log(err);
}
};
function getResult(connection, id) {
return new Promise((resolve, reject) => {
const sql = "SELECT link FROM document WHERE id=?";
connection.query(sql, id, function (err, result) {
if (err) reject(err);
const link = result[0].link;
const doc_name = link.split("/").pop();
resolve({ key: id, value: doc_name });
});
});
}
get_doc_name()
.then(dict => console.log(dict))
.catch(console.log);

Related

JavaScript - Async/Await - How to Return result from nested functions?

I'm new to Node.js and having difficulty working with async/await model. The issue I'm having is with nested function calls returning data in an async/await method, I always get 'undefined'. My functions structure is like this:
const findCustomerOrder = async (id) => {
const orders = await Order.find({custoemrId:id})
.then(async (order) => {
if(order)
{
const products = await findProductsByOrderId(order._id)
.then(async (result) => {
for(const r in result){
console.log("Product ID: ", result._id);
}
});
}
else
console.log("order not found");
});
}
const findProductsByOrderId = async (id) => {
try{
const products = await Products.find({orderId:id}, (error, data) => {
if(error) console.log(error);
else
return data;
});
}
catch(err){
return 'error!!';
}
}
I understand that if the top-level call is async/await then all the nested calls should be awaited as well that I've tried to do.
What am I doing wrong here?
Get rid of all the then, and also of the callback in the DB query. Use only await. It will make the whole code a lot easier to reason about and it should also solve your issue as part of the restructuring.
In the example below I also added a loop over the orders, because you are fetching all orders, as array, but then your code was behaving as if it got only one. I also fixed the products loop.
However, the way you fetch products doesn't seem to be right anyway, see my comment below. I didn't fix it because I don't know how your database structure looks, so I don't know what the right way would be.
async function findCustomerOrder (id) {
const orders = await Order.find({ customerId: id })
for (const order of orders) {
console.log(`Order ID: ${order._id}`)
const products = await findProductsByOrderId(order._id)
for (const product of products) {
console.log(`Product ID: ${product._id}`);
}
}
}
async function findProductsByOrderId (id) {
// Note: I don't think this is right. It seems to find all
// products with the given ID - which will always be one -
// and that ID would be the _product_ ID and not the order ID.
return await Products.find({ _id: id })
}
Preemptive comment reply: The return await is intentional, and this is why.
You should remove the then and also the callback and you can do something like below
const findCustomerOrder = async(id) => {
try {
const orders = await Order.find({
custoemrId: id
});
if (orders) {
let promiseArray = [];
orders.forEach(order => {
promiseArray.push(findProductsByOrderId(order._id));
});
let results = await Promise.all(promiseArray);
return results;
}
return "order not found";
} catch (err) {
throw err;
}
}
const findProductsByOrderId = async(id) => {
try {
const products = await Products.find({
_id: id
});
return products;
} catch (err) {
throw err;
}
}

How can I update mysql2 query to not return undefined for return?

I am using nodejs and mysql2. I am storing all my queries inside a class. When I console.log the results I am able to view the data correctly, however when I use a return statment to return the data, I am getting undefined. I believe I need to use promises, but am uncertain how to do so correctly. Here is what I have currently which is returning undefined,
viewManagerChoices() {
const sql = `SELECT CONCAT(first_name, ' ', last_name) AS manager, id FROM employee WHERE manager_id IS NULL`;
db.query(sql, (err, rows) => {
if (err) throw err;
const managers = rows.map(manager => ({ name: manager.manager, value: manager.id }));
managers.push({ name: 'None', value: null });
return managers;
});
};
This is my attempt at using promises which is returning as Promise {<pending>},
viewManagers() {
return new Promise((resolve, reject) => {
const sql = `SELECT CONCAT(first_name, ' ', last_name) AS manager FROM employee WHERE manager_id IS NULL`;
db.query(sql,
(error, results) => {
if (error) {
console.log('error', error);
reject(error);
}
const managers = [];
for (let i = 0; i < results.length; i++) {
managers.push({ name: results[i].manager, value: i+1 });
}
managers.push({ name: "None", value: null });
resolve(managers);
}
)
})
}
My class is called Query and I am calling these methods by doing,
const query = new Query();
query.viewManagerChoices();
query.viewManagers();
Your implementation for viewManagers is correct however promises don't make calls synchronous.
Either you need to use then callback or await the result in async context.
const query = new Query();
query.viewManagers().then((managers) => {
// do stuff here
}).catch((error) => console.error(error.message));
or
async someFunc() {
const query = new Query();
try{
const managers = await query.viewManagers();
}catch(error){
console.error(error.message);
}
}
Once you use promise you cannot just get a returned value without async/await or then flag. Once it's a promise the flow continues as the original.
For example:
// This is promise too because it has async flag.
// You cannot make it sync if you use promise in it
async myFunc(){
const const query = new Query();
const managers = await query.viewManagers();
return managers;
}
// It actually returns Promise<...>
// Now if you want to use myFunc in another function
// You need to do it the same way again
async anotherFunc(){
const something = await myFunc();
return something; // Returns promise
}
You can read more about promises here

Await sql and push to array before executing the result

I'm using mysql npm package to get data from the database.
My goal is to add every list(id) from lists Table and push it into lists array.
I'm getting the correct data from the database but when I result the query lists array is empty.
I think that I have to add async and await to the code to make it work. Tried in several places but I didn't make it work. Any idea what I'm doing wrong?
// GET - get all grocery_lists
Grocery_list.getAll = (result) => {
let lists = []; // <--- List Array
sql.query("SELECT id FROM lists", (err, res) => { // <--- Get all id from 'lists' table
if (err) {
console.log(err);
}
res.forEach(list => { // <--- Loop thru all lists
sql.query(`
SELECT items.id, items.name, items_x_lists.purchased
FROM items_x_lists
INNER JOIN items ON items_x_lists.itemId = items.id
WHERE items_x_lists.listId = ${list.id};
`, (err, res) => { // <--- Get all items for ${list.id} from 'items' table
if (err) {
console.log(err);
}
const list = {};
list.id = res.id;
console.log(list); // <--- { id: 1 } ... { id: 2 }
lists.push(list);
});
});
result(null, lists); // <--- returning empty array instead of [{ id: 1 }, { id: 2 }]
});
};
I think you can do it simply like this (not the exact code) but I think you've got the idea:
// GET - get all grocery_lists
Grocery_list.getAll = (result) => {
getData().then(data => {
// call result(data);
}).catch(err => {
// handle error for no data or any other errors
})
}
const getData = async () => {
try {
var res = await sql.query("SELECT id FROM lists");
// Handle error if no data found
if (!res) { Promise.reject("No data found"); }
} catch (error) {
return Promise.reject(error);
}
const listIds = res.map(id => id); // array of ids
try {
var data = await sql.query(`
SELECT items.id, items.name, items_x_lists.purchased
FROM items_x_lists
INNER JOIN items ON items_x_lists.itemId = items.id
WHERE items_x_lists.listId in ${listIds};`);
// Handle error if no data found
if (!data) { return Promise.reject("No data found") }
} catch (error) {
return Promise.reject(error);
}
return Promise.resolve(data)
}
this mighht be the solution
Grocery_list.getAll = async (result) => { return sql.query("SELECT id FROM lists", (err, res) => { // <--- Get all id from 'lists' table if (err) { console.log(err); } }); };
I’m outside, so I can only edit with my mobile phone. If the layout is strange, I apologize for it.
First of all, if it is me, I will adjust a few places
1.Create an async function and use Promise.all technology
async function queryAll(querys){
return await Promise.all(querys);
}
2.Create a Promise function to execute each sql
const queryPromise = id => {
return new Promise((resolve, reject) => {
sql.query(
'SELECT items.id, items.name, items_x_lists.purchased FROM items_x_lists INNER JOIN items ON items_x_lists.itemId = items.id WHERE items_x_lists.listId = ? ;',
id,
(err, rows, fields) => {
console.log(rows);
if (err) reject(err);
else resolve(rows);
}
);
});
};
3.Adjust the internal logic of the getAll event
const querys = []
res.forEach(list => {
querys.push(queryPromise(list.id));
});
const querysResults = await queryAll(querys);// remember to use async for function
querysResults.forEach(querysResult => {
lists.push({id:querysResult['id']});
});
Because there is no computer on hand, there may be some situations that need to be judged, but roughly this will work normally.
Hope this helps you :)

Get users info from nextcloud api

Using the node-nextcloud module, how to get emails for all users from an array? For example, the nc.users.get() method works with one userID, but what about when it's a hundred userIDs? I tried to work with the method directly and through a loop, but the result is not satisfactory.
const fs = require('fs')
const NextCloud = require('node-nextcloud');
var nc = NextCloud("Domain", "User", "Password");
var arr = []
//get users id into array
nc.users.list(null, (err, ocs)=> {
if (err) {
console.error(err);
}
arr = ocs.data.users
console.log(arr)
})
//try to get email for each id from array
nc.users.get('' , (err, ocs)=>{
if (err) {
console.log(err);
}
arr.forEach(email => {
console.log(ocs.data.email)
})
})
I'm not familiar with next cloud but it would appear to me you want to use the list method first to get all the user ids, then loop through the result from that list to get each user. Depending on the number of users it could be quite a lengthly operation. You're currently calling two asynchronous methods but the second one doesn't wait for the first one to finish. As I say I'm not familiar with next cloud but the below is a rough stab at what you might need
async function getUsers() {
let users = await nc.users.list();
const userEmails = await Promise.all(users.map(async (userId) => {
const user = await nc.users.get(userId);
return user.email;
}))
console.log("user emails", userEmails);
}
getUsers();
I managed to achieve the result in the following way:
nc.users.list(null, (err, ocs)=> {
if (err) {
console.error(err);
}
arr = ocs.data.users
//console.log(arr)
arr.forEach((item, index, array) => {
nc.users.get(`${item}`, (err, ocs) => {
if (err) {
console.error(err);
}
console.log(ocs.data.email);
})
})
})

Javascript function change variable value

I am currently developing signup page and I want to check if email address is already exist in the database.
var emailnum = email_num(`select * from contactinfo where email='${email}'`);
console.log(emailnum); //the output presents Promise { <pending> } not a num.
function sqlExecute(q) {
return new Promise((resolve, reject) => {
db.pool.query(q, function(err, result) {
if (err) {
console.log("ERROR ON " + q+"\n");
reject(err)
}
console.log("SUCCESS ON " + q);
resolve(result);
})
})
.catch(err => {
console.log(err);
})
}
//run check sql
async function email_num(tempquery){
var result = await sqlExecute(tempquery);
return result.rowCount;
}
I tried multiple ways but still could not figure it out.
I would appreciate any help TT
when I console.log, output is Always Promise { }.
I tried
var emailnum = email_num(`select count(*) as count from contactinfo where email='${email}'`)
.then((val)=>{
return val
};
console.log("number"+ emailnum);
The problem here is that you're not allowing the promise to resolve before attempting to retrieve the row count. When you create a function using async the return result is always going to be a promise. Here are a few solutions that will get you your desired result:
Solution 1: Use console.log to print the result of the promise after it has been resolved.
const email = 'something#gmail.com'
const numOfEmailsQuery = `SELECT * FROM contactinfo WHERE email = '${email}'`
email_num(numOfEmailsQuery)
.then(console.log)
.catch(console.error)
Solution 2: Use await inside of an async function to resolve the result of the promise and store it in a variable. Then print the result using console.log
async function printNumberOfEmails(email) {
const numOfEmailsQuery = `SELECT * FROM contactinfo WHERE email = '${email}'`
try {
const numOfEmails = await email_num(numOfEmailsQuery)
console.log(numOfEmails)
} catch (err) {
console.error(err)
}
}
const email = 'something#gmail.com'
printNumberOfEmails(email)
Hope that helps! Good luck!
First off, it's good practice to use prepared statements instead.
Also, you should just care about the count, so instead of select *, do select count(*) as count. Then you'll need check either the number of rows that result returned or the first index of rows, then the count property of that.
It should look something like this
function fetchEmailExists(email) {
const statement = 'select count(*) as count from contactinfo where email = $1';
return new Promise((resolve, reject) => {
db.pool.query(statement, [ email ], function(error, result) {
if (!error && result.rows.length > 0) {
resolve(result.rows[0]['count'] > 0);
} else {
reject(error);
}
});
})
.catch(error => {
console.log(error);
});
}
// Here is your call to fetchEmailExists
fetchEmailExists('test123#example.com')
.then(boolean => console.log(boolean));

Categories