I need to do the the same request to external api with differents parameter (node.js and express.js)
If all answers to my requests are correct, then I send the angular client code 200, however, if any erroneous send him the error.
I try something like that but don't work:
for(var i = 0; i<array.length; i++){
github.repos.createHook({
user: user,
repo: array[i],
name: "web",
config: {
url: "http://9ec2067d.ngrok.io/api/v1/callback",
content_type: "json"
},
events: ["*"],
active:true,
headers: {
"X-GitHub-OTP": "two-factor-code"
}
}, function(err, res) {
if (err) {
console.log(err);
errGithub = true;
response.status(404).json(err);
}
else {
if(i==array.length && !errGithub){
response.json({message:"OK"});
}
}
Any idea?
Many thanks
try this out:
// Include the async package
// Make sure you add "async" to your package.json
async = require("async");
// 1st para in async.each() is the array of items
async.each(array,
// 2nd param is the function that each item is passed to
function(item, callback){
// Call an asynchronous function,
github.repos.createHook({
user: user,
repo: item,
name: "web",
config: {
url: "http://9ec2067d.ngrok.io/api/v1/callback",
content_type: "json"
},
events: ["*"],
active:true,
headers: {
"X-GitHub-OTP": "two-factor-code"
}
}, function(err, res) {
if (err) {
console.log(err);
errGithub = true;
}
callback(); //required
});
},
// 3rd param is the function to call when everything's done
function(err){
if(err){
console.log('Error:' + err);
}
// All tasks are done now
if(errGithub ===true){
response.status(404).json(err);
}else{
response.json({message:"OK"});
}
}
);
You can use Promises, it is a clean and easy way to do it.
First create a queue with promises:
var queue = array.map((item) => {
return new Promise((resolve, reject) => {
github.repos.createHook({
user: user,
repo: item,
name: "web",
config: {
url: "http://9ec2067d.ngrok.io/api/v1/callback",
content_type: "json"
},
events: ["*"],
active:true,
headers: {
"X-GitHub-OTP": "two-factor-code"
}
}, (err, res) => {
if (err) {
reject(err)
console.log(err)
} else {
resolve(res);
}
})
})
})
Then execute it:
Promise.all(queue).then((values) => {
response.json({message:"OK"})
},(err) => {
response.status(404).json(err)
});
This example just explains how you could use promises, the code has not been tested.
check also the documentation on: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
Related
I am trying to get the response data from 3 APIs which is depended on each other. when I call each external API the response has an array of external APIs, as I need all the 3 external APIs responses.
Here is the code:
const options = {
JSON: true,
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
"Authorization": process.env.authID,
},
body: {},
uri: ''
};
app.post('/sync', async (req, res) => {
try {
let getFirstAPI = Object.assign({}, options);
getFirstAPI.uri = 'https://abcde'; // first API
request(getFirstAPI, function (err, httpResponse, body) {
Promise.all(body.projects.map((prct) => getVuln(prct))).then(rs => {
res.JSON({ message: rs });
});
});
}
});
async function getVuln(prodId) {
try {
let getSecondAPI= Object.assign({}, options);
getSecondAPI.uri = 'abcd' + prodId.id + '/abc'; //Second API
return await new Promise((resolve, reject) => {
request(getSecondAPI, function (err, httpResponse, body) {
let final = {
products: [],
dataList: []
}
final.products.push({
product_id: prodId.id,
product_name: prodId.abc,
product_ver: prodId.wdc
})
body.issues.forEach(csa => {
final.dataList.push({
product_id: prodId.id,
version: csa.Versions,
name: csa.Name,
pathJsonValue:await getPathfun(csa.link) //Here i am getting error "SyntaxError: Unexpected identifier"
});
});
resolve(final);
});
})
}
}
function getPathfun(urlStr) {
let getPathUrl = Object.assign({}, options);
getPathUrl.url = urlStr;
return new Promise((resolve, reject) => {
request(getPathUrl, function (err, httpResponse, body) {
resolve(body);
});
});
}
at the "pathJsonValue" I am getting an error while running the code, if I remove the await then "pathJsonValue" is empty. I have attached the error image below. please help me with this issue.
request(getSecondAPI, function (err, httpResponse, body) {
Callback must be an ASYNC function to have the possibility to use 'await' keyword inside it.
'await' keyword doesnt work in "forEach" method. Use for...of loop instead.
I am trying to store API results into an array.
The data is displayed in console, but on pushing the data into an array, the array is still empty.
Here's the code:
app.post('/fetchFavoriteTweets/', verifyToken, function(req, res) {
var favorites = [];
dbConn.then( function (database) {
var dbo = database.db("twitter_search");
dbo.collection('users').findOne(
{ _id: ObjectId(req.userId) }, function(err, result) {
if(err) throw err;
if(!result.hasOwnProperty('favorite_tweets')) {
res.status(404).json({msg:'record not found'});
}
else {
result.favorite_tweets.forEach(function (tweet) {
T.get('statuses/show', {id: tweet.id}, function(err, data, response) {
if(!err){
favorites.push(data);
console.log(data); //this returns data
} else {
console.log(err);
}
});
});
console.log(favorites);
// res.status(200).json({msg:'success', data:favorites});
}
});
}).catch(function(e){console.log(e)})
});
It looks like you're defining the favorites array within the scope of the function callback. Try putting var favorites = []; above you app.post() call instead.
Also, keep in mind that it will only have a value after the callback is complete, so any synchronous code later down the line will only see the empty array value.
I've updated your code to get favorites by storing separately the promise and call it afterwards:
UPDATE
As you can see in the demo, i have 2x console.log at the bottom, the first one(C1) is contained in the promise favoritesPromise () and the second (C2) is after the promise.
Synchronous actions will never wait for asynchronus actions to take place, therefore in my example C2 will always be outputted before C1, even if console.log(1 ... ) is before console.log(2 ... ), they'll appear reversed in the console.
In the promise i added a setTimeout of 1ms to mock a request, it was all it took to achieve the current output. Another thing you can test is removing the setTimeout then output will change a bit, your promise becomes synchronus until it reaches resolve(favorites), that means favorites has all the data by now, but when resolve takes place, it becomes async, and in your console you will still see C2 first (but now with data) and C1 second.
In my earlier answer i tried to implement this reasoning to your code.
Keep it async folks!
var favorites = [];
var favoritesPromise = () => {
return new Promise((resolve, reject) => {
console.log('Retrieving data from the internet.');
// This timeout mocks your request to anything that is async or promie
setTimeout(() => {
console.log('Request done')
let resultFavorite_tweets = [{
id: 1,
name: 'a dog'
}, {
id: 2,
name: 'a cat'
}];
resultFavorite_tweets.forEach(item => {
favorites.push(item.name);
})
resolve(favorites);
// if you have an error use
// reject(err)
}, 1);
});
}
favoritesPromise().then(favList => {
console.log(1, 'this will always contain data from the internet, but will always be last', favList);
})
console.log(2, 'this will be empty (unless you remove setTimeout), but will always be first', favorites);
app.post('/fetchFavoriteTweets/', verifyToken, function(req, res) {
const favoritesPromise = () => {
return new Promise((resolve, reject) => {
var favorites = [];
dbConn.then(function(database) {
var dbo = database.db("twitter_search");
dbo.collection('users').findOne({
_id: ObjectId(req.userId)
}, function(err, result) {
if (err) reject(err);
if (!result.hasOwnProperty('favorite_tweets')) {
res.status(404).json({
msg: 'record not found'
});
} else {
result.favorite_tweets.forEach(function(tweet) {
T.get('statuses/show', {
id: tweet.id
}, function(err, data, response) {
if (!err) {
favorites.push(data);
console.log(data); //this returns data
} else {
console.log(err);
reject(err);
}
});
resolve(data);
});
console.log(favorites);
// res.status(200).json({msg:'success', data:favorites});
}
});
}).catch(function(e) {
reject(e)
})
});
}
// Here you call the promise to retrieve "favorites"
favoritesPromise().then(favoritesList => {
console.log('your favorites array', favoritesList)
})
})
Try next code
app.post('/fetchFavoriteTweets/', verifyToken, function (req, res) {
var favorites = [];
dbConn.then(function (database) {
var dbo = database.db("twitter_search");
dbo.collection('users').findOne(
{ _id: ObjectId(req.userId) }, function (err, result) {
if (err) throw err;
if (!result.hasOwnProperty('favorite_tweets')) {
res.status(404).json({ msg: 'record not found' });
}
else {
// Counter
let count = result.favorite_tweets.length;
result.favorite_tweets.forEach(function (tweet) {
T.get('statuses/show', { id: tweet.id }, function (err, data, response) {
// Decrease count
count -= 1;
if (!err) {
favorites.push(data);
// Check if count is zero
if (count === 0) {
console.log(favorites);
res.status(200).json({msg:'success', data:favorites});
}
} else {
console.log(err);
}
});
});
}
});
}).catch(function (e) { console.log(e) })
});
I know this topic as already asked many times before but I didn't find the right answer to do what I want.
Actually, I try to save two different list of JSON object in MongoDB via Mongoose. To perform both at the same time I use 'async'.
However, when I save it with the command insertMany() I get an error because he calls the callback of async before finishing the insertMany(). Therefore answer[0] is not defined.
What will be the proper way of doing it ?
Here is my code with the async:
const mongoose = require("mongoose");
const async = require("async");
const utils = require("../utils");
const experimentCreate = function(req, res) {
let resData = {};
let experimentList = req.body.experiment;
let datasetList = req.body.datasetList;
async.parallel(
{
dataset: function(callback) {
setTimeout(function() {
answer = utils.createDataset(datasetList);
callback(answer[0], answer[1]);
}, 100);
},
experiment: function(callback) {
setTimeout(function() {
answer = utils.createExp(experimentList);
callback(answer[0], answer[1]);
}, 100);
}
},
function(err, result) {
if (err) {
console.log("Error dataset or metadata creation: " + err);
sendJSONresponse(res, 404, err);
} else {
console.log("Experiment created.");
resData.push(result.dataset);
resData.push(result.experiment);
console.log(resData);
sendJSONresponse(res, 200, resData);
}
}
);
};
Then the two functions called createExp and createDataset are the same in another file. Like this:
const createDataset = function(list) {
let datasetList = [];
for (item of list) {
let temp = {
_id: mongoose.Types.ObjectId(),
name: item.name,
description: item.description,
type: item.type,
};
datasetList.push(temp);
}
Dataset.insertMany(datasetList, (err, ds) => {
if (err) {
console.log("Error dataset creation: " + err);
return [err, null];
} else {
console.log("All dataset created.");
return [null, ds];
}
});
};
There's a few problems with your code. For one, you're not returning anything in your createDataset function. You're returning a value in the callback of insertMany but it doesn't return that value to the caller of createDataset as it's within another scope. To solve this issue, you can wrap your Dataset.insertMany in a promise, and resolve or reject depending on the result of Data.insertMany like this:
const createDataset = function(list) {
let datasetList = [];
for (item of list) {
let temp = {
_id: mongoose.Types.ObjectId(),
name: item.name,
description: item.description,
type: item.type,
};
datasetList.push(temp);
}
return new Promise((resolve, reject) => {
Dataset.insertMany(datasetList, (err, ds) => {
if (err) {
console.log("Error dataset creation: " + err);
reject(err);
} else {
console.log("All dataset created.");
resolve(ds);
}
});
});
};
Now your return object is no longer going to be an array so you won't be able to access both the error and the result via answer[0] and answer[1]. You're going to need to chain a then call after you call createDataset and use callback(null, answer) in the then call (as that means createDataset executed successfully) or use callback(err) if createDataset throws an error like below:
dataset: function(callback) {
setTimeout(function() {
utils.createDataset(datasetList).then(answer => {
callback(null, answer);
}).catch(err => callback(err)); // handle error here);
}, 100);
}
Note: You'll most likely need to alter your createExp code to be structurally similar to what I've produced above if it's also utilizing asynchronous functions.
I am trying to make a rest call and update a list and then resolve the promise with the updated list.
function addTestCaseToTestRail(){
return new Promise(function(resolve){
compareTestRailAndProtractor().then(function(tests){
var testsLength = tests.tests.length;
var url = testRailURL+testRailData.addTestEndPoint;
for(var i=0; i<testsLength; i++){
if(tests.tests[i].id==undefined){
var newId=""
var options = {
url:url,
headers:headers,
body:{
"title":tests.tests[i].name,
"custom_jira_component" : 465
},
json: true
}
request.post(options, function(err, httpResponse, body){
if (err) {
console.error(err);
return;
}
newId = body.id;
});
tests.tests[i].id = newId;
}
}
resolve(tests);
});
});
}
function test(){
addTestCaseToTestRail().then(function(tests){
console.log(tests);
});
}
test()
The request is getting posted and I am able to create tests in test rail but the resolve(tests) does not have the newId assignment.
This is the output I am getting. Not sure why resolve does not wait for the rest call to complete.
{ tests:
[ { id: '', name: 'test1'},
{ id: '', name: 'test2'},
{ id: '', name: 'test3'},
{ id: '', name: 'test4'},
{ id: '', name: 'test6'},
{ id: '', name: 'test5'} ] }
compareTestRailAndProtractor returns a Promise. You can use async/await within .then() and Promise constructor within for loop to await request callback, which is issue at code at Question, as the for loop does not await the callback function
function addTestCaseToTestRail() {
return compareTestRailAndProtractor()
.then(async function(tests) {
var testsLength = tests.tests.length;
var url = testRailURL + testRailData.addTestEndPoint;
for (var i = 0; i < testsLength; i++) {
await new Promise((resolve, reject) => {
if (tests.tests[i].id == undefined) {
var newId = ""
var options = {
url: url,
headers: headers,
body: {
"title": tests.tests[i].name,
"custom_jira_component": 465
},
json: true
}
request.post(options, function(err, httpResponse, body) {
if (err) {
reject(err);
}
newId = body.id;
tests.tests[i].id = newId;
resolve();
});
} else {
resolve()
}
});
}
return tests
})
}
function test() {
addTestCaseToTestRail()
.then(function(tests) {
console.log(tests);
})
.catch(function(err) {
console.error(err)
})
}
test()
I think it's because of the non-blocking nature of Javascript. The thing is that "resolve(test)" gets executed before your "post" event has a response (which is the one where you take the Id from).
I would recommend you to take a look at https://caolan.github.io/async/ Async is an amazing library for handling async processes.
Good luck!
EDIT: You can also take a look at async/await JS operators
const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-central-1' })
const createDocument = (text, callback) => {
const createParams = {
Item: {
text: text
},
TableName: 'ToDoItems'
}
docClient.put(createParams, (err, data) => {
if(err) {
callback(err, null)
} else {
callback(null, data)
}
})
}
exports.handle = (event, context, callback) => {
createDocument(event.text, (err, data) => {
if(err) {
callback(err, null)
} else {
callback(null, data)
}
})
}
That's my AWS Lambda function, the issue is that when I get a callback, data object is empty, even though document is inserted into DynamoDB. What could the issue be here?
You can't. You have to separately query. On put, if you set ReturnValues: 'ALL_NEW', then you'll get "ReturnValues can only be ALL_OLD or NONE"
Note the 'possible' in AWS's documentation:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
ReturnValues — (String) Possible values include:
"NONE"
"ALL_OLD"
"UPDATED_OLD"
"ALL_NEW"
"UPDATED_NEW"
Also, instead of separately querying, you can also just use the params value. If it was saved, then what you have in createParams.Item is basically what's returned if you were to separately query.
There is a workaround - You can use update method of DynamoDB.DocumentClient.
TableName: "table",
Key: {
id: randomId
},
AttributeUpdates: {
authorId: {Action: "PUT", Value: event.authorId},
date: {Action: "PUT", Value: event.date},
description: {Action: "PUT", Value: event.description},
title: {Action: "PUT", Value: event.title}
},
ReturnValues: "ALL_NEW"
This method will create new item and return all what you need
You have to request the return values, like this:
const createParams = {
Item: {
text: text
},
TableName: 'ToDoItems',
ReturnValues: 'ALL_NEW'
}
This is documented here.
I did have to implementing that the return in .then() was params.Item, like this:
var params = {
TableName:table,
Item:{
"name": value,
"email": value2,
}
};
console.info("Adding a new item...");
await docClient.put(params)
.promise()
.then(data => {
return params.Item;
}).catch(error => {
console.error(error)
throw new Error(error)
})