How to check if database entry (table row) already exists - javascript

I am trying to check whether a table entry exists in a database, but what I have so far always returns true even if there is no entry. What am I doing wrong?
Thank you all.
This will always console.log >>>> true
let myPersLocalEntityExistsPromise = function (localEntityGUID) {
return new Promise(function (resolve, reject) {
let myEntityExists = true;
const client = myDb.mySQLDbLocalConnection();
let stmt = "SELECT EXISTS(SELECT 1 FROM MY_ENTITY_TABLE WHERE LOCAL_MY_ENTITY_GUID = ?)";
let todo = [
localEntityGUID
];
client.query(stmt, todo, function (err, row) {
if (err) {
return ("myPersLocalEntityExists: " + err.message);
} else {
if (row && row.length) {
console.log(localEntityGUID + ' Case row was found!');
} else {
myEntityExists = false;
console.log(localEntityGUID + ' Case row was NOT found!');
}
}
});
client.end(function (err) {
if (err) {
console.log('ERRR');
}
});
if (myEntityExists) {
resolve(myEntityExists);
} else {
reject(myEntityExists);
}
})
}
function myPersLocalEntityExists(localEntityGUID, callback) {
let responsePromises = []
responsePromises.push(myPersLocalEntityExistsPromise(localEntityGUID))
Promise.all(responsePromises).then(fromResolve => {
console.log(">>>> " + fromResolve);
return fromResolve;
}).catch(fromReject => {
console.log(">>>> " + fromReject);
return fromReject;
});
}

Related

Break/return in a for loop of async calls?

Currently my function looks like this:
const checkDynamoID = (dynamoClient, tableName) => {
function runCheck(client, params, id) {
return new Promise(resolve => {
client.getItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
if (data.Item === undefined) {
console.log("Key available: " + id)
resolve(id)
} else {
console.log("Key taken: " + id)
}
}
});
});
}
for (let i = 0; i < 10; i++) {
const randomID = this.generateRandomBase64URL()
const dynamoParams = {
TableName: tableName, //TABLE_NAME
Key: {
'videoID': { S: randomID },
}
}
const res = runCheck(dynamoClient, dynamoParams, randomID).then((result) => {
console.log("Result: " + result)
if (typeof result !== 'undefined') {
console.log("Result: " + result)
return result
}
})
}
return ""
}
My goal is to run runCheck, which makes async calls to a DynamoDB to check for an ID, in a for loop until it returns a valid ID. After that, I'd like to return or break out of the external for loop that encloses the call and return from checkDynamoID. However, I'm not sure how to do this; I tried running this and it does not return, even after generating a valid value (ie. typeof result !== 'undefined').
Is there a way of doing this?

Can I use try catch like this in node.js?

I'm making node server, to show up all elements in specific folder.
and have to stop function when path === seekPath,
can I throw result data and catch it to show all elements?
app.get('/view/:id', function(req, res){
if (req.session.key) {
const rawData = fs.readFileSync(__folderTree + req.params.id + ".json")
const jsonData = JSON.parse(rawData)
const seekPath = __storage + req.params.id + "/" + req.query.path
console.log(seekPath)
try {
seekFolder(jsonData, seekPath)
} catch (result) {
showElements(result)
}
}
else {
res.redirect('/login');
}
})
function seekFolder(jsonData, seekPath) {
jsonData.children.forEach(element => {
if(element.type === 'directory'){
console.log("compare " + element.path + " & " + seekPath)
if(element.path === seekPath){
throw(element.children)
}
else{
seekFolderElements(element, seekPath)
}
}
});
}
function showElements(jsonArrData) {
jsonArrData.forEach(element => {
if(element.type === 'directory'){
console.log("!!"+element.name)
showElements(element)
}
else{
console.log("!!"+element.name)
}
});
}

My arr is empty when I return it with a function

I'm trying to pass arrEntree in my return, however it's inside a promise. How would I fix this issue? The function query iniaites a new promise but like everytime ArrEntree is printed empty, because it gets called before the promise is finished processing. How would I finish the promise then only call my return...? How would I solve this issue.
function query(statement, params, first) {
return new Promise(function(resolve, reject) {
connection.query(statement, (params || []), function(err, rows) {
if (err) {
console.log("err", err);
if (err.code === "ETIMEDOUT") {
connectToDb();
return query(statement, params, first);
}
reject(err);
} else {
if (first) {
resolve(rows[0]);
} else {
resolve(rows);
}
}
});
});
}
function sendPlanEmails(){
if (plans.length === 0) {
return true;
}
const orders = plans.map(plan=>{
const arrEntree = []
const planData = srcData.plans[plan.productId];
const entrees = plan.entrees;
const entreeID = Object.keys(plan.entrees);
query('select * from entrees where entree_id in (' + entreeID.join(',') + ')')
.then(function(results){
results.forEach(function(element){
entreeID.forEach(function(second_elem){
if(element.entree_id==second_elem){
console.log('worked until here')
const quantity = plan.entrees[second_elem];
const name = element.entree;
arrEntree.push({name:name, quantity:quantity});
}
})
})
})
return {
days:plan.days.days,
planDataTitle:planData.title,
breakfast:plan.meals[0],
lunch:plan.meals[1],
dinner:plan.meals[2],
entreeArry:arrEntree
};
});
const template_mp = srcData.orderPage.mailchimp_template_mp || 'order_confirmation';
return utils.sendMealPlanEmail(template_mp, {
from: from,
order: orders,
});
}

Do I use promises correctly?

I'm working on a school project right now, I want to use promises for my "matching functions" but apparently I'm doing this wrong.
I have a dating website where users can meet other people and "match" with them. This is the idea. I'm doing a suggestion thing so it will suggest profiles automatically. So far, I can suggest people for 'straight users' but here i'm trying to do it for 'bi users' and I can't make it work.
I have a function called getPotentialsLocation(), it works perfectly if i'm giving it my table of 'straight users' (straightPotentials) but it can get into my other condition (else if (biPotentials))
Why ? Everyone around me tried but we can't find any answer.
So if I'm trying to get 'straight users' it will return me the rows with the location and everything. If I'm trying to 'bi users' it will block because it can't get into the other condition event though I can console.log(biPotentials) all the way up.
I hope my code makes sense, if it doesn't please tell me. Because I'm going to link a lot of code here
router.post('/matchaSearch', function (req, res) {
var username = session.uniqueID;
var searcherInfos = {};
var straightPotentials = [];
var biPotentials = [];
function suggestUsers(){
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
var query = 'SELECT sex, orientation FROM usersinfo WHERE username = ?';
connection.query(query, [username], (err, rows, fields) => {
connection.release()
return err ? reject(err) : resolve(rows)
})
})
})
}
suggestUsers()
.then((rows) => {
searcherInfos = rows;
if (searcherInfos[0].sex === 'm' && searcherInfos[0].orientation === 's'){
console.log("searcher is straight");
lookForSF()
.then((rows) => {
if (rows) {
for (var i = 0; i < rows.length; i++) {
straightPotentials.push(rows[i].username)
}
if (straightPotentials){
console.log("straightPotentials" + straightPotentials);
getPotentialsLocation()
.then((rows) => {
console.log(rows);
}).catch((err) => {
throw err;
})
}
}
}).catch((err) => {
throw err;
});
} else if ((searcherInfos[0].sex) && searcherInfos[0].orientation === 'b'){
console.log("searcher is bi");
lookForbothCauseImB()
.then((rows) => {
if (rows) {
for (var i = 0; i < rows.length; i++) {
biPotentials.push(rows[i].username)
}
if (biPotentials){
console.log("biPotentials" + biPotentials);
getPotentialsLocation()
.then((rows) => {
console.log(rows);
}).catch((err) => {
throw err;
})
}
}
}).catch((err) => {
throw err;
})
}
//this is the .catch for my first function (suggestUsers())
}).catch((err) => {
throw err;
})
function lookForSF(){
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
var query = 'SELECT username FROM usersinfo WHERE sex = "f" AND orientation = "s" AND username != ?';
connection.query(query, [username], (err, rows, fields) => {
connection.release()
return err ? reject(err) : resolve(rows)
})
})
})
}
function lookForbothCauseImB(){
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
var query = 'SELECT username FROM usersinfo WHERE sex = "f" OR sex = "m" AND orientation = "s" OR orientation = "b" AND username != ?';
connection.query(query, [username], (err, rows, fields) => {
connection.release()
return err ? reject(err) : resolve(rows)
})
})
})
}
function getPotentialsLocation(){
if (straightPotentials) {
var string = '('
for (var i =0; i <straightPotentials.length - 1; i++){
string += '\'' + straightPotentials[i] + '\', ';
}
string += '\'' + straightPotentials[straightPotentials.length - 1] + '\')';
return new Promise((resolve, reject) =>{
pool.getConnection((err, connection) => {
var query = 'SELECT * FROM userlocation WHERE username IN ' + string;
console.log("this is our query " + query);
connection.query(query, (err, rows, fields) => {
connection.release();
return err ? reject(err) : resolve(rows)
})
})
})
} else if (biPotentials) {
var string = '('
for (var i =0; i <biPotentials.length - 1; i++){
string += '\'' + biPotentials[i] + '\', ';
}
string += '\'' + biPotentials[biPotentials.length - 1] + '\')';
console.log("this is string" + string);
return new Promise((resolve, reject) =>{
pool.getConnection((err, connection) => {
var query = 'SELECT * FROM userlocation WHERE username IN ' + string;
console.log("this is our query " + query);
connection.query(query, (err, rows, fields) => {
connection.release();
return err ? reject(err) : resolve(rows)
})
})
})
}
}
})
as you suppose, you don't.
the mistake is nested promises anti-pattern (google it)
Your piece of code is huge and need a massive refactor to be set in the right way
Here you are
function query (pool, sql, values) {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
var query = sql
connection.query(query, values, (err, rows) => {
connection.release()
err ? reject(err) : resolve(rows)
})
})
})
}
function suggestUsers (pool, username) {
return query(pool, 'SELECT sex, orientation FROM usersinfo WHERE username = ?', [username])
}
function lookForSF (pool, username) {
return query(pool, 'SELECT username FROM usersinfo WHERE sex = "f" AND orientation = "s" AND username != ?', [username])
}
function lookForbothCauseImB (pool, username) {
return query(pool, 'SELECT username FROM usersinfo WHERE sex = "f" OR sex = "m" AND orientation = "s" OR orientation = "b" AND username != ?', [username])
}
function getPotentialsLocation (pool, potentials) {
var usernames = potentials.map((potential) => {
return "'" + potential + "'"
})
return query(pool, 'SELECT * FROM userlocation WHERE username IN (' + usernames.join(',') + ')')
}
function matchaSearch (pool, username) {
return suggestUsers(pool, username)
.then((searcherInfos) => {
if (searcherInfos[0].sex === 'm' && searcherInfos[0].orientation === 's') {
return lookForSF(pool, username)
} else if ((searcherInfos[0].sex) && searcherInfos[0].orientation === 'b') {
return lookForbothCauseImB(pool, username)
}
})
.then((rows) => {
var potentials = rows.map((row) => {
return row.username
})
console.log('potentials' + potentials)
return getPotentialsLocation(pool, potentials)
})
}
router.post('/matchaSearch', function (req, res) {
matchaSearch(pool, session.uniqueID)
.then((results) => {
console.log('result', results)
res.send(JSON.stringify(results))
})
.catch((err) => {
console.error('error', err)
res.status(500).send('something wrong')
})
})
I see at least two places where you have functions within a .then chain that do not return anything.
This will break the promise chain.
See e.g.:
lookForbothCauseImB().then(...)
and
getPotentialsLocation().then(...)
both of which appear to need a return in front of them.

Node.js mysql query passing to next function in async.waterfall

I have some code in node.js using express. I route a call to request data from a mysql database, and what I want to do is pass that to another function to restructure the returned json from a tabular form (table query) to a hierarchy type json.
I've tested the script separately to restructure the output from my sql query. However, I am having trouble passing it from my query function to my new script(function)
I'm just not seeing what I am doing wrong. Any help please and thanks.
exports.get_site_menu = function (req, res) {
var dbc;
console.log('In menu setup');
async.waterfall([
// get a connection
function (callback) {
db.db(callback);
}
,
querylookup, modifyjson
], completed);
function querylookup(dbclient, res) {
dbc = dbclient;
dbc.query("SELECT categories, " +
"subcategories, " +
"pid, " +
"title, " +
"description " +
"FROM MENU_SELECT_ACTIVE_VIEW " +
"where company_id = ? and site_id = ?", [req.query.companyid, req.query.siteid], res);
}
function modifyjson(err, res) {
categories = [];
console.log('results ' + res);
res.forEach(function (entry) {
var cindex = categories.map(function (category) {
return category.name;
}).indexOf(entry.categories);
console.log(cindex);
if (cindex < 0) {
// Not found in categories array
cindex = categories.push({
name: entry.categories,
subcategories: []
}) - 1; // -1 to fix the index
}
// Lets search the subcategory
var category = categories[cindex];
var sindex = category.subcategories.map(
function (subcategory) {
return subcategory.name;
}
).indexOf(entry.subcategories);
if (sindex < 0) {
// Not Found
sindex = category.subcategories.push({
name: entry.subcategories,
items: []
}) - 1;
}
// Subcategory exists. Just push
category.subcategories[sindex].items.push({
pid: entry.pid,
description: entry.description,
title: entry.title
});
});
menu = {
menu: {
categories: categories
}
};
console.log('menu ' + menu);
}
function completed(err, menu, fields) {
if (dbc) dbc.end();
if (err) {
callback(err);
} else {
console.log(menu);
res.contentType('json');
res.send(JSON.stringify(menu));
}
}
};
You need to pass each result to own callback to pass next function. I have refactored your code like;
exports.get_site_menu = function (req, res) {
var dbc;
console.log('In menu setup');
async.waterfall([
// get a connection
function (callback) {
db.db(callback, some_result);
},
function querylookup(dbclient, res, callback) {
dbc = dbclient;
dbc.query("SELECT categories, " +
"subcategories, " +
"pid, " +
"title, " +
"description " +
"FROM MENU_SELECT_ACTIVE_VIEW " +
"where company_id = ? and site_id = ?", [req.query.companyid, req.query.siteid], res);
callback(null, result_from_queryLookup);
},
function modifyjson(err, res, callback) {
categories = [];
console.log('results ' + res);
res.forEach(function (entry) {
var cindex = categories.map(function (category) {
return category.name;
}).indexOf(entry.categories);
console.log(cindex);
if (cindex < 0) {
// Not found in categories array
cindex = categories.push({
name: entry.categories,
subcategories: []
}) - 1; // -1 to fix the index
}
// Lets search the subcategory
var category = categories[cindex];
var sindex = category.subcategories.map(
function (subcategory) {
return subcategory.name;
}
).indexOf(entry.subcategories);
if (sindex < 0) {
// Not Found
sindex = category.subcategories.push({
name: entry.subcategories,
items: []
}) - 1;
}
// Subcategory exists. Just push
category.subcategories[sindex].items.push({
pid: entry.pid,
description: entry.description,
title: entry.title
});
});
menu = {
menu: {
categories: categories
}
};
console.log('menu ' + menu);
callback(null, menu, fields);
}
], function completed(err, menu, fields) {
if (dbc) dbc.end();
if (err) {
callback(err);
} else {
console.log(menu);
res.contentType('json');
res.send(JSON.stringify(menu));
}
});
};
Especially, be careful on callback parts.

Categories