Handling chained and inner promise not properly executing - javascript

I believe my promises aren't being finished because I'm not handling them correctly. At the end of my code, within Promise.all(), console.log(payload) is displaying {}. When it should display something like this:
{
project1: {
description: '...',
stats: {python: 50, css: 50}
},
project2: {
description: '...',
stats: {python: 25, css: 75}
},
project3: {
description: '...',
stats: {python: 10, css: 90}
}
}
code:
app.get("/github", (req, res) => {
const authorizationHeader = {headers: {Authorization: 'Basic ' + keys.github.accessToken}};
const user = 'liondancer';
const githubEndpoint = 'api.github.com/repos/';
var payload = {};
let promises = req.query.projects.map(project => {
let datum = {};
const githubAPIUrl = path.join(githubEndpoint, user, project);
return fetch('https://' + githubAPIUrl + '/languages', authorizationHeader).then(res => {
// Get Languages of a project
if (!isStatus2XX(res)) {
throw 'Status code not 2XX:' + res.status;
}
return res.json();
}).then(res => {
let languagePercentages = {};
let total = 0;
// get total
Object.keys(res).forEach(key => {
total += Number.parseInt(res[key]);
});
// compute percentages
Object.keys(res).forEach(key => {
languagePercentages[key] = (Number.parseInt(res[key]) / total * 100).toFixed(1);
});
datum.stats = languagePercentages;
// Get description of a project
fetch('https://' + githubAPIUrl).then(res => {
if (!isStatus2XX(res)) {
throw 'Status code not 2XX: ' + res.status;
}
return res.json();
}).then(res => {
datum.description = res.description;
payload[project] = datum;
});
}).catch(err => {
console.log('Github API error: ' + err);
});
});
Promise.all(promises).then(() => {
console.log(payload);
res.send(payload);
}).catch(err => {
console.log('nothing ever works...: ' + err);
});
});
At first I replaced .map with .forEach() to have the code execute and the code seemed to have worked properly. payload had the values I expected. However, now that I want to send the aggregated results, I cant seem the properly execute the promises in the correct order or if at all.

just change this line
fetch('https://' + githubAPIUrl).then(res => {
into this
return fetch('https://' + githubAPIUrl).then(res => {
so promise.all will resolve after all nested promises have resolved so payload filled up.

Related

Get value of variable outside API function call [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 10 months ago.
I am trying to make a chat application project with abusive text detection. I found code for the chat application online and want to add text detection using Perspective API. The API has several attributes for toxicity, threat etc. I am able to set the attributes inside the API function but I am unable to access them outside it.
Here is the relevant code:-
const sendMessage = asyncHandler(async (req, res) => {
const { content, chatId } = req.body;
let toxicity, insult, profanity, threat;
if (!content || !chatId) {
console.log("Invalid data passed into request");
return res.sendStatus(400);
}
let newMessage = {
sender: req.user._id,
content: content,
chat: chatId,
toxicity: toxicity,
insult: insult,
profanity: profanity,
threat: threat,
};
let inputText = newMessage.content;
// Perspective API
google
.discoverAPI(process.env.DISCOVERY_URL)
.then((client) => {
const analyzeRequest = {
comment: {
text: inputText,
},
requestedAttributes: {
TOXICITY: {},
INSULT: {},
PROFANITY: {},
THREAT: {},
},
};
client.comments.analyze(
{
key: process.env.API_KEY,
resource: analyzeRequest,
},
(err, response) => {
if (err) throw err;
// console.log(JSON.stringify(response.data, null, 2));
toxicity = (response.data.attributeScores.TOXICITY.summaryScore.value * 100).toFixed(2);
insult = (response.data.attributeScores.INSULT.summaryScore.value * 100).toFixed(2);
profanity = (response.data.attributeScores.PROFANITY.summaryScore.value * 100).toFixed(2);
threat = (response.data.attributeScores.THREAT.summaryScore.value * 100).toFixed(2);
newMessage.toxicity = toxicity;
newMessage.insult = insult;
newMessage.profanity = profanity;
newMessage.threat = threat;
console.log("1-" + newMessage.toxicity); // This returns the desired output
}
);
})
.catch((err) => {
throw err;
});
//
console.log("2-" + newMessage.toxicity); // This returns undefined
try {
let message = await Message.create(newMessage);
message = await message.populate("sender", "name profilePic");
message = await message.populate("chat");
message = await User.populate(message, {
path: "chat.users",
select: "name profilePic email",
});
await Chat.findByIdAndUpdate(req.body.chatId, {
latestMessage: message,
});
res.json(message);
} catch (error) {
res.status(400);
throw new Error(error.message);
}
});
I want newMessage to be updated after the API call. After coming across this post, I found that console.log("2-" + newMessage.toxicity) executes before console.log("1-" + newMessage.toxicity). I tried using callbacks and async/await but couldn't make it work.
The console.log("2-" + newMessage.toxicity) is outside the google.discoverAPI call so it execute instantly.
you can try something like this
const sendMessage = asyncHandler(async (req, res) => {
const { content, chatId } = req.body;
let toxicity, insult, profanity, threat;
if (!content || !chatId) {
console.log("Invalid data passed into request");
return res.sendStatus(400);
}
let newMessage = {
sender: req.user._id,
content: content,
chat: chatId,
toxicity: toxicity,
insult: insult,
profanity: profanity,
threat: threat,
};
let inputText = newMessage.content;
// Perspective API
const client = await google
.discoverAPI(process.env.DISCOVERY_URL)
const analyzeRequest = {
comment: {
text: inputText,
},
requestedAttributes: {
TOXICITY: {},
INSULT: {},
PROFANITY: {},
THREAT: {},
},
};
await new Promise((resolve, reject) => {
client.comments.analyze(
{
key: process.env.API_KEY,
resource: analyzeRequest,
},
(err, response) => {
if (err) {
reject(err)
}
// console.log(JSON.stringify(response.data, null, 2));
toxicity = (response.data.attributeScores.TOXICITY.summaryScore.value * 100).toFixed(2);
insult = (response.data.attributeScores.INSULT.summaryScore.value * 100).toFixed(2);
profanity = (response.data.attributeScores.PROFANITY.summaryScore.value * 100).toFixed(2);
threat = (response.data.attributeScores.THREAT.summaryScore.value * 100).toFixed(2);
newMessage.toxicity = toxicity;
newMessage.insult = insult;
newMessage.profanity = profanity;
newMessage.threat = threat;
console.log("1-" + newMessage.toxicity);
resolve()
}
);
})
.catch((err) => {
throw err;
});
//
console.log("2-" + newMessage.toxicity); // This returns undefined
try {
let message = await Message.create(newMessage);
message = await message.populate("sender", "name profilePic");
message = await message.populate("chat");
message = await User.populate(message, {
path: "chat.users",
select: "name profilePic email",
});
await Chat.findByIdAndUpdate(req.body.chatId, {
latestMessage: message,
});
res.json(message);
} catch (error) {
res.status(400);
throw new Error(error.message);
}
});

Call a method inside Promise.all from Redux Saga

In the code block below, var ret=that.sendSMTPEmailForOrderPlaced(orderData); is not getting executed. The console is printing "before calling," but it is not printing "inside sendSMTPEmailForOrderPlaced" message. Getting error TypeError: Cannot read property 'sendSMTPEmailForOrderPlaced' of null in createNewOrderHistory method.createNewOrderHistory is called from Redux Saga
const result = yield call(MyProfileRepository.createNewOrderHistory, data);
What is wrong with the code below?
class MyRepository {
constructor(callback) {
this.callback = callback;
}
createNewOrderHistory(tableData) {
var that = this;
const AuthStr = 'Bearer ' + getToken();
let promises = [];
tableData.map((tableData, index) => {
var data = {
invoice_id: tableData.invoiceID.toString(),
};
promises.push(axios.post(`url`, data, {
headers: { Authorization: AuthStr },
}));
});
return Promise.all(promises).then(function(results) {
console.log("before calling")
var ret = that.sendSMTPEmailForOrderPlaced(orderData);
console.log("after calling")
console.log(ret);
return (results);
}).catch(error => {
return (error);
});
}
sendSMTPEmailForOrderPlaced(data) {
console.log("inside sendSMTPEmailForOrderPlaced")
const response = axios.post(`url`, data).then((response) => {
return response.data;
}).catch((error) => {
console.log(error);
return (error);
});
return response.data;
return null;
}
}
export default new MyRepository();
It's hard to test your code, but I believe that #Keith had the right idea in his comment. So to test it I had to change 'url' and so on. But this code should give you a good idea on how to write it:
const axios = require('Axios');
class MyRepository {
async createNewOrderHistory(tableData) {
var that = this;
const AuthStr = 'Bearer '; // + getToken();
const header = { headers: { Authorization: AuthStr } };
let promises = tableData.map((tableData, index) => {
var data = { invoice_id: tableData.invoiceID.toString() };
return axios.post('https://jsonplaceholder.typicode.com/posts', data, header);
});
const results = await Promise.all(promises).then(async (results) => {
console.log("before calling")
var ret = await that.sendSMTPEmailForOrderPlaced(results.data);
console.log("after calling", ret);
return (results);
}).catch(error => {
return (error);
});
console.log(results.map(a => a.data));
}
async sendSMTPEmailForOrderPlaced(data) {
console.log("inside sendSMTPEmailForOrderPlaced")
try {
const response = await axios.post('https://jsonplaceholder.typicode.com/posts', data);
return response.data;
} catch (error) {
return error;
}
}
}
var repo = new MyRepository();
repo.createNewOrderHistory([{ invoiceID: 'test' }, { invoiceID: 'test2' }, { invoiceID: 'test3' }]);
If you want to run this, past it into a test.js file in an empty folder, then run the following in the same folder:
npm init -y
npm i axios
node .\test.js

How to push data to realtime database using a loop

I am trying to push data into the realtime database using a for loop as there are multiple entries. The am confused on how should multiple promises be handled. Please help.
onSubmit() {
for(let i = 0; i < this.userList.length; i++) {
this.mtcService.getUserCount(this.userList[i].$key).subscribe(
((ct) => {
const Mtcount = ct.length;
// pushing to realtime db =>
this.mtcService.createUser(this.userList[i].$key, Mtcount, this.userForm.value)
.then(() => {
console.log('Success ' + i);
}, err => {
console.log(err);
})
}),
((err) => {
console.log(err);
})
);
}
}
getUserCount(id) {
return this.db.list('path1/path2/' + id).snapshotChanges();
}
createUser(path, count, data) {
return this.db.object('path3/path4/' + path + '/' + count).set(data);
}
const promises = data.map(async (id) => {
await Axios.post(URL);
});
await Promise.all(promises);

Firebase response: undefined is not an object

I'm trying to save data into Firebase storage.
Generally, my method and function works, in FireBase logs I get:
Function execution took 1442 ms, finished with status code: 201
alert15
alert14: null||[object Object]
alert12:
alert11:
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove
these restrictions
Function execution started
My function:
const functions = require('firebase-functions');
const cors = require("cors")({origin: true});
const fs = require("fs");
const UUID = require("uuid-v4");
const gcconfig = {
projectId: "myrojectid",
keyFilename: "mykeyfile.json"
};
const gcs = require("#google-cloud/storage")(gcconfig);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.storeImage = functions.https.onRequest((request, response) => {
console.log("alert11: ")
cors(request, response, () => {
console.log("alert12: ")
const body = JSON.parse(request.body);
fs.writeFileSync("/tmp/uploaded-image.jpg", body.image, "base64", err => {
console.log(err => console.log("alert13: " + err));
return response.status(500).json({error: err})
});
const bucket = gcs.bucket("myapp.appspot.com");
const uuid = UUID();
bucket.upload(
"/tmp/uploaded-image.jpg",
{
uploadType: "media",
destination: "/places2/" + uuid + ".jpg",
metadata: {
metadata: {
contentType: "image/jpeg",
firebaseStorageDownloadTokens: uuid
}
}
}, (err, file) => {
console.log("alert14: " + err + "||" + file)
if (!err) {
console.log("alert15");
response.status(201).json({
imageUrl:
"https://firebasestorage.googleapis.com/v0/b/" +
bucket.name +
"/o/" +
encodeURIComponent(file.name) +
"?alt=media&token=" +
uuid
})
} else {
console.log("alert16: ")
console.log(err);
response.status(500).json({error: err})
}
});
});
});
My method:
import {ADD_PLACE, DELETE_PLACE} from './actionTypes';
export const addPlace = (placeName, location, image) => {
return dispatch => {
fetch("https://us-central1-myapp.cloudfunctions.net/storeImage", {
method: "POST",
body: JSON.stringify({
image: image.base64
})
})
.catch(err=> console.log(err))
.then(res => {res.json(); console.log("alert2 " + {res})})
.then(parsedRes => {
console.log("alert1: " + parsedRes);
const placeData = {
name: placeName,
location: location,
image: parsedRes.imageUrl
};
return fetch("https://myapp.firebaseio.com/places.json", {
method: "POST",
body: JSON.stringify(placeData)
}).catch(err => console.log("alert13: " + err))
})
.catch(err => console.log("alert4", err))
.then(res => res.json())
.catch(err => console.log("alert5: " + err))
.then(parsedRes => {
console.log("alert6", parsedRes);
}).catch(err => console.log("alert17: " + err));
};
};
export const deletePlace = (key) => {
return {
type: DELETE_PLACE,
placeKey: key
};
};
but in local console in my IDE I got this:
alert1: undefined
'alert4', { [TypeError: undefined is not an object (evaluating 'parsedRes.imageUrl')]
I wasted 3 days for this and still 0 progress.
What can be wrong here ? How to fix it ?
You're not using promise chaining correctly. You need to explicitly return the result of a then() callback to the next handler in the chain. Without returning anything, the next then() callback will get undefined. For example:
.then(res => {res.json(); console.log("alert2 " + {res})})
In this line of code, you're not returning anything to pass along to the next handler in the chain.
In fact, the above then() callback is unnecessary because it's not kicking off any other async work. You could just call res.json() in the subsequent then() block, just before the second fetch. You typically only add another then() block when you have more async work to do as a result of the prior async work.

Node.js call callback function inside for loop

I am trying to call a function inside a for loop and the problem is that the function is called after the loop was finished.
Taking the below as an example, it prints to the console:
here1
here1
here2
here2
Instead of
here1
here2
here1
here2
report.forEach(item => {
item.runs.forEach(run => {
waComplianceBusiness(req, run.id, (err, res) => {
const compliance = res.data.overviews[0].compliance;
var failureList = [];
compliance.forEach((rule, index) => {
console.log('here1');
waRuleOverview(req, run.id, rule.id, (err, res) => {
console.log('here2');
// handle the response
});
});
});
});
});
How can I fix this?
Please let me know if I need to provide additional information
Here is the complete code:
export default (req, callback) => {
const report = req.body.webAudits;
if(report.length > 0) {
report.forEach(item => {
item.runs.forEach(run => {
waComplianceBusiness(req, run.id, (err, res) => {
const compliance = res.data.overviews[0].compliance;
if(compliance) {
var failureList = [];
compliance.forEach((rule, index) => {
if(rule.pagesFailed > 0) {
waRuleOverview(req, run.id, rule.id, (err, res) => {
const failedConditions = res.data.failedConditions;
const ruleName = res.data.ruleName;
failedConditions.forEach((condition, failedIndex) => {
const request = {
itemId: condition.conditionResult.id,
itemType: condition.conditionResult.idType,
parentId: condition.conditionResult.parentId,
parentType: condition.conditionResult.parentType
}
const body = {
runId: run.id,
ruleId: rule.id,
payload: request
}
waConditionOverview(req, body, (err, res) => {
const description = res.data.description;
const conditionValues = res.data.conditionValues[0];
var actualValue = conditionValues.value;
if(actualValue == "") {
actualValue = 'empty';
}
if(description.idType == "variable") {
var failureObj = {
ruleName: ruleName,
expected: description.name + ' ' + description.matcher + ' ' + description.expected[0],
actual: description.name + ' ' + description.matcher + ' ' + actualValue
};
}
else if(description.idType == "tag") {
var failureObj = {
ruleName: ruleName,
expected: description.name + '\n' + description.matcher,
actual: actualValue
};
}
failureList.push(failureObj);
});
});
});
}
if(key + 1 == compliance.length) {
console.log(failureList);
}
});
}
});
});
});
}
}
These are the callback functions:
export function waComplianceBusiness(req, runId, callback) {
const apiToken = req.currentUser.apiToken;
const payload = {
'Authorization': 'api_key ' + apiToken
}
const options = {
'method': 'get',
'gzip': true,
'headers': payload,
'content-type': 'application/json',
'json': true,
'url': 'api_url'
}
request(options, (error, response, body) => {
callback(null, body);
});
}
export function waRuleOverview(req, runId, ruleId, callback) {
const apiToken = req.currentUser.apiToken;
const payload = {
'Authorization': 'api_key ' + apiToken
}
const options = {
'method': 'get',
'gzip': true,
'headers': payload,
'content-type': 'application/json',
'json': true,
'url': 'api_url'
}
request(options, (error, response, body) => {
callback(null, body);
});
}
export function waConditionOverview(req, body, callback) {
const apiToken = req.currentUser.apiToken;
const payload = {
'Authorization': 'api_key ' + apiToken
}
const options = {
'method': 'post',
'gzip': true,
'headers': payload,
'body': body.payload,
'content-type': 'application/json',
'json': true,
'url': 'api_url'
}
request(options, (error, response, body) => {
callback(null, body);
});
}
My goal is to return the failureList array after the loop over the compliance array is done
I found a similar question here but not sure if that would work in my case and I don't really know how to implement the promises
The for loop executes the statements inside the scope sequentially. But it does not wait for the the function calls to complete, it continues with the next statement(i.e works asynchronously). That is why the result is as such. You can make it work synchronously using Promises or by using the async module.
As it is not clear what you are going to perform in the function call and what you want the statements to do, I am not able to suggest either of which. . asyn.each is usually preferred for making the for loop execute synchronously. And promises are used when you want to wait for the function to finish executing and then perform operation. You might want to look at their documentation
Promises|MDN
async.each
Thank you, Ragul
If you want to do it in sequence use async.eachOfSeries
async.eachOfSeries(report, function(item, index, eachOfCallback1){
async.eachOfSeries(item.runs, function(run, index, eachOfCallback2){
waComplianceBusiness(req, run.id, (err, res) => {
var failureList = [];
async.eachOfSeries(compliance, function(rule, index, eachOfCallback3){
console.log('here1');
waRuleOverview(req, run.id, rule.id, (err, res) => {
console.log('here2');
return eachOfCallback3(err);
});
}, function(err){
if(err)
return eachOfCallback2(err);
else return eachOfCallback2();
});
});
}, function(err){
if(err)
return eachOfCallback1(err);
else return eachOfCallback1();
})
}, function(err){
// handle final response
})
If you want to optimise the process take a look at async.parallel

Categories