Node js - How to return array multidimensional - javascript

I need to create an array with this structure:
[
{
position: 2,
family: 9404,
part: [ 'article1', 'article2', 'article3' ]
},
{
position: 3,
family: 9405,
part: [ 'article4', 'article5', 'article6' ]
}
]
So i have a form where i select the parts that i want and send the families to get url.In the getter function i do a for to get the articles of each family and i want to query a select of articles and a select of positions. After that i try to push each array to a main array but i can't, show me undefined. How can i do this kind of operations?
I'm new with node and express and this is the first time that i have to do that.
My code:
getFamilies(req, res)
{
console.log(req.params.data);
var parsedData = JSON.parse(req.params.data);
var compounds = parsedData[0].compounds;
var supplier = parsedData[0].supplier;
var families = parsedData[0].families;
console.log(parsedData[0].compounds.length);
var position = [];
var data = [];
var parts = [];
for (var i = 0; i < compounds.length; i++)
{
parts.push(request.query("SELECT st.ref, st.design FROM st WHERE familia ='"+families[i]+"'"));
position.push(request.query("SELECT u_order FROM u_part WHERE u_familia='"+families[i]+"'"));
}
return Promise.all(parts, position, families).then(function(listOfResults)
{
//add parts, position and families to data[]
var data = [];
//console.log(data);
console.log(listOfResults);
console.log("done");
//return listOfResults;
res.render('view2', {teste: data});
}).catch(function(err)
{
// ... query error checks
console.log(err);
});
}
In promise just print the first parameter "parts" and if i put the [parts, position, families] give me promise pending.
And how can i put the data in the structure that i show above.
parseData:
[
{
"compounds": ["8"],
"supplier": ["sup"],
"families": ["9305"]
}
]
Please teach me how can i do this kind of operations.
Thank you

Not sure why you're passing families to Promise.all families seems to just be an array of data from taken from the query
Promise.all takes an array of promises in input, and you're passing an array of arrays of promises and of data...
you should never build SQL queries like this. This is a big flaw for SQL injection (but that's another question)
So do:
Promise.all([...parts, ...position]) or if you're not using ES6 syntax Promise.all(parts.concat(position))
and fix your SQL!
====
Final code could look like:
getFamilies = (req, res) => {
var families = JSON.parse(req.params.data)[0].families;
var positions = [];
var data = [];
var parts = [];
families.forEach(family => {
// see http://stackoverflow.com/a/7760578/2054629 for mysql_real_escape_string
parts.push(request.query("SELECT st.ref, st.design FROM st WHERE familia ='"+mysql_real_escape_string(family)+"'"));
positions.push(request.query("SELECT u_order FROM u_part WHERE u_familia='"+mysql_real_escape_string(family)+"'"));
});
return Promise.all([Promise.all(parts), Promise.all(positions)]).then(listOfResults => {
var [partResult, positionResult] = listOfResults;
var data = families.map((family, i) => {
var pos = positionResult[i];
var parts = partResult[i];
return {family: family, position: pos, parts: parts};
});
res.render('view2', {teste: data});
}).catch(err => {
// ... query error checks
console.log(err);
});
};

You incorrectly use Promise.all, it takes array of promises
return Promise.all([Promise.all(parts), Promise.all(position)]).then(function(listOfResults){
var partResult = listOfResults[0];
var positionResult = listOfResults[1];
var data = [];
for (var i=0; i<families.length; i++) {
var family = families[i];
var pos = positionResult[i];
var parts = partResult; // logic to extract parts for current family
data.push({family: family, position: pos, parts: parts})
}
console.log(data);
})

Promise.all() takes an array of promises, it looks like you are passing multiple arrays.

Related

Iterating over results of papa.parse object

I believe this may be an issue with async or promises, however, this concept has been a struggle for me to grasp. I have spent much time researching questions relating to papaparse and promises, but believe I am still not grasping some simple concept.
Overview
In the below code, a .csv file is gathered from the user, converted into an Object using PapaParse, then distance/duration values are added to the data from GoogleMaps Distance Matrix and finally, the intent is to return this data back to the user as a .csv containing this combined information.
In downloadCSV(), I am able to log the Object which results from getDistTime. However, I am unable to iterate over this object or send it to papa.unparse to convert to a .csv. Below the code, I have attached an image of the structure of arr within downloadCSV().
I have spent much time researching questions here, and on the papaparse repo. I would appreciate any help on how to best proceed.
Contents of arr:
Code
$("#submit").click(function(){
//REMOVE THIS LINE BEFORE LIVE OR VALIDATION WILL BE DUN GOOFED
event.preventDefault();
getData();
});
function getData(){
var csv = document.getElementById('exampleFormControlFile1').files[0];
// Parse CSV string
Papa.parse(csv, {
header: true,
complete: function(results) {
var final_rows = getDistTime(results)
downloadCSV(final_rows)
}
});
} // end getData
function downloadCSV(arr){
//console.log(arr) shows the results
// but trying to iterate over arr or unparsing shows undefined
console.log(arr)
csv = Papa.unparse(arr)
...more csv export code
}
function getDistTime(resources){
var rows = []
const promiseArr = [];
for (var i = 0; i < resources.data.length; i++) {
var origin1 = $("#citystate").val();;
var destinationA = resources.data[i]['DEMOBILIZATION CITY'] + ',' + resources.data[i]['DEMOBILIZATION STATE'];
promiseArr.push(googleRequest(origin1, destinationA));
}
Promise.all(promiseArr)
.then((resultsArr) => {
resultsArr.forEach((result, i) =>
pushToRows(resources.data[i], result, rows));
})
return rows
} // end getDistTime
function pushToRows(resources, dist_dur, rows){
resources["DISTANCE_MI"] = dist_dur[0];
resources["ACTUAL_DUR_HR"] = dist_dur[1];
resources["FINANCE_DUR_HR"] = (dist_dur[0] / 45.0).toFixed(2)
rows.push(resources)
} // end pushToRows
getDistTime is performing an async action and therefore rows is returning empty before the Promise.all has resolved as you wont have pushed any data to it until that promise resolves.
Looks like you'll need to await the result of getDistTime or change how get data works, not sure you're using async await so simplest way is to just return that Promise.all and then only download the csv once its returned and resolved in your complete callback.
for example you could do
function getData(){
var csv = document.getElementById('exampleFormControlFile1').files[0];
// Parse CSV string
Papa.parse(csv, {
header: true,
complete: function(results) {
getDistTime(results).then(finalRows => downloadCSV(finalRows));
}
});
} // end getData
function getDistTime(resources){
var rows = []
const promiseArr = [];
for (var i = 0; i < resources.data.length; i++) {
var origin1 = $("#citystate").val();;
var destinationA = resources.data[i]['DEMOBILIZATION CITY'] + ',' + resources.data[i]['DEMOBILIZATION STATE'];
promiseArr.push(googleRequest(origin1, destinationA));
}
return Promise.all(promiseArr)
.then((resultsArr) => {
resultsArr.forEach((result, i) =>
pushToRows(resources.data[i], result, rows)
);
return rows
})
} // end getDistTime

Pass array values as parameter to function and create json data

I have a scenario where I am passing an array of objects to a function in nodejs, but the same is failing with undefined error.
Here is what I have tried :
var object = issues.issues //json data
var outarr=[];
for(var key in object){
outarr.push(object[key].key)
}
console.log(outarr) // array is formed like this : ['a','b','c','d','e']
for(var i =0; i<outarr.length;i++){
jira.findIssue(outarr[i]) //here I am trying to pass the array objects into the loop one by one
.then(function(issue) {
var issue_number = issue.key
var ape = issue.fields.customfield_11442[0].value
var description = issue.fields.summary
var ice = issue.fields.customfield_15890[0].value
var vice = issue.fields.customfield_15891.value
var sor = issue.fields.labels
if (sor.indexOf("testcng") > -1) {
var val = 'yes'
} else {
var val = 'yes'
}
var obj = {};
obj['ape_n'] = ape;
obj['description_n'] = description;
obj['ice_n'] = ice;
obj['vice_n'] = vice;
obj['sor_n'] = val;
var out = {}
var key = item;
out[key] = [];
out[key].push(obj);
console.log(out)
} })
.catch(function(err) {
console.error(err);
});
});
What I am trying to achieve : I want to pass the array values as a parameter which is required by jira.findissue(bassically passing the issue number) one by one and which should again fetch the values and give a combine json output.
How can I pass this array values one by one in this function and also run jira.findissue in loop.
Any help will be great !! :-)
I have taken a look at the code in your question.
To be honest the code you wrote is messy and contains some simple syntax errors.
A good tip is to use a linter to avoid those mistakes.
More info about linters here: https://www.codereadability.com/what-are-javascript-linters/
To output all results in one array you have to define the array outside the scope of the loop.
I cleaned the code a bit up and use some es6 features. I don't know the context of the code but this is what I can make off it:
//map every value the key to outarr
let outarr = issues.issues.map( elm => elm.key);
//Output defined outside the scope of the loop
let output = [];
//looping outarr
outarr.forEach( el => {
jira.findIssue(el).then(issue => {
//creating the issue object
let obj = {
ape_n: issue.fields.customfield_11442[0].value,
description_n: issue.fields.summary,
ice_n: issue.fields.customfield_15890[0].value,
vice_n: issue.fields.customfield_15891.value,
sor_n: issue.fields.labels.indexOf("testcng") > -1 ? "yes" : "yes",
};
//pushing to the output
output[issue.key] = obj;
}).catch(err => {
console.log(err);
});
});
//ouputing the output
console.log(output);
Some more info about es6 features: https://webapplog.com/es6/

How to run a function for each item in an array or write array objects individually on Mongoose

Hi everyone I'm trying to create an API for calculating revenue from a calculator service, all it essentially does is grab the values from the dB that the calculator needs and pass them through it and get the resulting values. I need to write the results back into the dB however for logging.
I'm having some problems however
I am trying to pass some objects from a Mongoose array to a JSON API URL service using axios get and I'm having trouble writing the resulting array to my Mongoose dB
Can anyone help me with what I'm missing?
See below
/* Users :
_id : 5cac
username: sample
email: email#email.com
orderHistory: [
{xmr_hr: 9000},
{xmr_hr: 2000},
{xmr_hr: 3000}
],
profitHistory:[
{
xmr_usd_gross:
}
]
*/
var email = "email#email.com"
var id = "5cac"
function xmr(callback){
function getUsers(){
var cursor = User.find({email: email}, function (err, users) {
}).cursor();
return cursor;
}
var cursor = getUsers();
cursor.on('data', function(name){
const xmr_hr= name.orderHistory.map(element => element.xmr_hr);
var xmrLength = xmr_hr.length;
for (var i = 0; i < xmrLength; i++) {
console.log(xmr_hr[i]
// returns [9000,2000,3000]
// variables
var algo = "cn8"
var hr = xmr_hr
var wttURL = "wtt.com/json" + algo + hr[i]
axios.get(wttURL)
.then((response) => {
var btcGrossRevenue = response.data.coins.Monero.btc_revenue
console.log(btcGrossRevenue)
// runs through each value in array and returns revenue [0.06, 0.02, 0.03]
// below is my problem
var updateProfits = [{
xmr_usd_gross : btcGrossRevenue,
}]
User.findByIdAndUpdate(id, {upsert: false, "$addToSet": { 'profitHistory': updateProfits}},
function(err, user) {
if(err) { console.log(err) }
else {
console.log(user.length)
};
})
})
}
// initial callback
for (var v = 1; v < 2; v++)
xmr(function(v){})
// cronJob and callback continuation
var xmrJob = new cron.CronJob('* 10 * * * *', function() {
console.log('Function executed!');
for (var v = 1; v < 2; v++) // amount of times to run function
xmr(function(v){})
}, null, true);
xmrJob.start
I know the problem is the fact that I'm trying to write an array to an object inside an array, but I'm not sure how to pass each object in the array individually.
I am thinking either there is a way to do it by just running the callback function individually for each object item in the array or find the ability to dissect the resulting array and write them sequentially.
I would like for my Collections to look like this once everything is written
/* Users :
_id : 5cac
username: sample
email: email#email.com
orderHistory: [
{xmr_hr: 9000},
{xmr_hr: 2000},
{xmr_hr: 3000}
],
profitHistory:[
{xmr_usd_gross: 0.06},
{xmr_usd_gross: 0.03},
{xmr_usd_gross: 0.02},
]
*/
Any help would be much appreciated
You can use the Map function.
const fn = (item) => {
return item *10;
}
[1,2,3,4,5].map( fn );
with this simple example, the function fn is executed on each item in the map and returns a value. If you assign it to something you can leverage that value resulting array.
I obviously seperated them out but you can easily do them inline as well.... or leverage the forEach function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
[1,2,3,4,5].map( item => item * 10 );
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
[1,2,3,4,5].forEach( item => {
console.log( item * 10 );
});
Your arrays can be anything, objects, numbers whatever, and similarly... you can do anything in the functions.

Understanding promises a little more

I'm struggling to make this works. i'm dealing with sequelize promisses and i want to return the queried elements to the view, but instead its returning null. I know its because promises are async requests and it does not return a result immediatly after you call it, ok, but how to return the values, put it into an array and than return the array?
this is the code i have so far.
router.post('/register', function(req, res, next) {
var sales = req.body.sales;
var person = req.body.personID;
var promisses = [];
var delivery = req.body.delivery;
for(var i =0;i<sales.length;i++){
var product_id = sales[i].product_id;
var amount = sales[i].amount;
var price = sales[i].produto_price;
var salePromisse = Sale.create({
'product_id': product_id,
'person_id': person,
'amount': amount,
'price': price,
'total': amount*price
});
//i couldnt find a word which describes what movimentacao means...lets keep it.
var movimentacao = Movimentacao.create({
"tipo": 0,
"id_product": product_id,
"id_provider": "",
"data": new Date(),
"price": amount*price
});
promisses.push(salePromisse);
promisses.push(movimentacPromisse);
}
Promise.all(promisses).then(function (promissesArray){
var name = "";
var suc = 0;
var total = 0;
var salesResult = [];
var salesObject = {};
if(!delivery){
res.send({msg: "aaaaa"});
}else{
promissesArray.forEach(function(pro){
if(pro!==null && pro !== undefined){
if(pro['$modelOptions'].tableName === undefined){
Product.findById(pro.product_id).then(function (product){
salesObject.product = product.name;
salesObject.price= pro.price;
salesObject.amount = pro.amount;
salesObject.total = pro.total;
total += salesObject.total;
salesResult.push(salesObject);
salesObject = {};
return Person.findById(per);
}).then(function (person) {
name = person.name;
});;
}
}
});
//here is where i would like to return to the view salesResult and name.
//res.render("folder/view", {sales: salesResult, person: name});
console.log(salesResult);
}
});
});
Well, the promisses array has the CREATE instance for each of my models, and i'm creating a few types of it.
I want to insert on the database, check if the promise resolved is dealing with an specific table (the field modelOptions is undefined on it, i already debugged), query all the other results because on the promisses array i have just the id, and than put into the array to be able to return to the view, but on the console.log on the last line, it returns null. How can i improve the code to be able to do all i want and than return to the view?
Dont worry, all the model related variables are beeing declared above.
Thanks.

closing mongodb connection and get correct results with multiple parallel asynchronous query

I am new to Mongo and Node. I am currently using Mongoskin and Bluebird to handle the db connection and queries (as suggested here: https://stackoverflow.com/a/23687958/2701348 ).
I have three collections: Users, Binders and Cards.
The Binders collection contains the information about Cards for each User.
Each document in Binders has as properties:
User Id <--- that refers to the User owning the Card
Card Code <--- that refers to a Card
Count <--- that refers to the number of cards owned by the User
I prefer to have a separate Cards collection so that when a Card changes, it changes for all the Users Binders.
Now I am willing to retrieve an array for a given user such as:
[{card: {card document}, count: 4}, ...]
I have the following problems:
the db connection should be closed after all the async db callbacks are called
the cards array should be returned after the last db.collection('cards').find gives back the results
I know my following code is wrong but can be a starting point for a discussion:
var getAllBinderCards = function(req, res){
var db = req.db;
var userId = req.userId;
var promiseBinders = db.collection('binders').find({userId: userId}).toArrayAsync();
promiseBinders.then(function(binderCards) {
if (binderCards) {
var promiseCards;
//console.log("------ binderCards: ", binderCards);
var cards = [];
for (var i = 0; i < binderCards.length; i++) {
var binderCard = binderCards[i];
promiseCards = db.collection('cards').find({Code: binderCard.Code}).toArrayAsync();
promiseCards.then(function(cardsDB){
if(cardsDB){
//console.log("Cards found: ",binderCard.Code, cardsDB);
for (var i = 0; i < cardsDB.length; i++) {
cardsDB[i].count = binderCard.count;
};
cards.concat(cardsDB);
}
});
}
promiseCards.then(function(){
db.close();
console.log("Binder Cards: " , cards);
res.json(cards);
});
}
});
}
I am struggling trying to figure out how to handle the promisfied asynchronous call correctly in order to send back the whole array and close the db connection.
I think I should try to build a promise before the for loop and use it to chain the query on Cards promises and lastly chain the db.close() and res.json(cards) statements.
[EDIT] Maybe the easiest solution is to simply use the $in filter inside a single db.collection('cards').find({Code: {$in: [bindersCodeArray] }}).toArrayAsync(); and avoid that for loop:
var getAllBinderCards = function(req, res){
var db = req.db;
var userId = req.userId;
var promiseBinders = db.collection('binders').find({userId: userId}).toArrayAsync();
promiseBinders.then(function(binderCards) {
if (binderCards) {
var binderCodes = binderCards.map(function(element){
return element.Code;
});
var promiseCards = db.collection('cards').find({Code: {$in: binderCodes} }).toArrayAsync();
promiseCards.then(function(cards){
var bindersDictionary = {};
for (var i = 0; i < binderCards.length; i++) {
bindersDictionary[binderCards[i].Code] = binderCards[i].count;
};
for (var i = 0; i < cards.length; i++) {
cards[i].count = bindersDictionary[cards[i].Code];
};
db.close();
console.log("Binder Cards: " , cards);
res.json(cards);
});
}
});
}
Still I am curious if there is an elegant way to solve this riddle using promises.
I would expect that using $in and array may have constraints on the number of binders you can pass and affect query performance. You can also try doing this with async#map. e.g.:
...
function(binders) {
async.map(binders, cardsForBinders, function(err, bindersWithCards) {
// TODO: close connection here.
}
function cardsForBinders(binder, callback) {
// 1. find cards for binder.
// 2. prepare transformed response: binderWithCards.
callback(null, binderWithCards);
}
}
...

Categories

Resources