forEach() iterates too fast with ASYNC AWAIT - Skipping data - foreach

I am re-writing my website and am trying to import my members from array of JSON objects.
Each object represents a band that the user has uploaded. So I wrote a quick file that works with the list and my new database / authentication system, auth0.
As outline in the comments below...
First I Request an access token so I can create an account for each member using Auth0. Then I iterate through my array of objects representing bands. For each band object I have the code do a few things... First, it creates a new account on my auth system. This sends back a user ID. I add the user ID to the band object to be stored in my database. It adds the location as well as adds a few posts that the user may have had from the old system.
This all works well if I do one user... But if I let it iterate through the whole array it doesn't work. It added about 15 bands to the auth system and 50 bands to the database... It seems like its going too fast to successfully publish them all. Any ideas on how to fix this?
** Also tried this with a for loop instead of for each. Moving everything inside the for loop creates both the same number of bands and user accounts, but now it times out at about 10 bands instead of doing all of them.
Thanks!
const uuidv4 = require('uuid')
const BulkBands = require('./BulkBands')
const fetch = require("node-fetch");
let bandCount = 0
//First I need to get an Access Token for my Auth Proider, Auth0
const getAccessToken = async () => {
try {
let response = await fetch(`https://nm-music.auth0.com/oauth/token`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: '{"client_id":"xxxxxxxxx","client_secret":"xxxxxxxxx", "audience":"https://xxxxxxxx.com/api/v2/","grant_type":"client_credentials"}'
})
const responseData = await response.json()
//Now I start The Process of Adding the Bands.
addTheBands(responseData.access_token)
} catch (error) {
console.log(error)
}
}
//Here I use the array method, forEach() to iterate over the list of about 77 bands.
const addTheBands = (accessToken) => {
BulkBands.forEach(band => {
//The first thing I want to do for each band is create an account for them on the Auth system.
createNewAccount(band, accessToken)
})
}
//Here is the function to create the account.
const createNewAccount = async (band, accessToken) => {
try {
const response = await fetch(`https://xxxxxxxxx.com/api/v2/users`, {
method: 'POST',
headers: {
'content-type': 'application/json;',
"Authorization": `Bearer ${accessToken}`,
},
body: JSON.stringify({
"connection": 'Username-Password-Authentication',
"email": band.bandEmail,
// "phone_number": "+19995550123",
"user_metadata": {},
"blocked": false,
"email_verified": false,
// "phone_verified": false,
"app_metadata": {},
// "given_name": band.bandName,
// "family_name": band.bandName,
"name": band.bandEmail,
// "nickname": "Johnny",
// "picture": "https://secure.gravatar.com/avatar/15626c5e0c749cb912f9d1ad48dba440?s=480&r=pg&d=https%3A%2F%2Fssl.gstatic.com%2Fs2%2Fprofiles%2Fimages%2Fsilhouette80.png",
// "user_id": "",
// "connection": "Initial-Connection",
"password": "xxxxxxxx",
"verify_email": false,
// "username": band.bandEmail
})
})
let newUserResponse = await response.json()
//Now that their account has been made, I want to add the band to my database, and use the Account ID from newUserResponse as the userId for the band. That way the band is associted with their account.
addTheBand({
quoteGenerator: [],
userId: newUserResponse.identities[0].user_id,
posts: [
{
"type": "band",
"data": "",
"date": new Date(),
"postId": uuidv4(),
"approved": null,
"rockOn": []
},
],
type: 'band',
bandName: band.bandName,
bandEmail: band.bandEmail,
bandGenre: band.bandGenre,
bandBio: band.bandBio,
youtube: [band.youtube1, band.youtube2],
published: false,
cancellationPolicy: 'You may cancel this performance up to 2 weeks prior to the performance date without penalty. If this performance is cancelled within 2 weeks of the performance date the booking party will be required to pay 50% of the booking fee to the band. If the booking is cancelled within 3 days of the performance date the full payment is required to be paid to the band. ',
baseCost: band.baseCost,
mainDate: {
charge: 0,
sunday: 0,
monday: 0,
tuesday: 0,
wednesday: 0,
thursday: 0,
friday: 0,
saturday: 0,
}
}, band)
} catch (error) {
console.log(error)
}
}
//Now to start adding the band...
const addTheBand = async (newBand, band) => {
let newLocation = []
let accessToken = ''
//I need to geo-locate each band for my new system. So I use the google api to do so.
try {
let resposne = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${band.bandLocation}&key=xxxxxxxxx`)
const responseLocation = await resposne.json()
newLocation = [responseLocation.results[0].geometry.location.lng, responseLocation.results[0].geometry.location.lat ]
// I store the geo location for the band and now I post the new band to my database. The Geo Location is Indexed and does not work if I post it - I need to do this after the band is posted - no clue why.
response = await fetch('http://localhost:5000/api/autoquotegenerators', {
method: 'POST',
headers: {
"Content-Type": 'application/json; charset=UTF-8'
},
body: JSON.stringify(newBand)
})
const responseData = await response.json()
if(responseData){
console.log('Band Added');
//Like I said before... I need to add the geo-location now. So this function will do so.
updateBandLocation({
geometry: {
coordinates: newLocation,
city: band.bandLocation,
}
}, responseData._id, responseData, band)
}
} catch (error) {
console.log(error)
}
}
//I send the geo-location data to this function... it also takes care of a few last minute things such as posts, and quote figures and such. It all works.
const updateBandLocation = async (newLocation, bandUserId, band, oldBand) => {
console.log('adding location...')
let newPosts = [...band.posts]
let newQuoteGenerator = []
if(oldBand.timedRate > 0 ){
newQuoteGenerator.push({
cardType: "timed",
inputType: "select",
cardTitle: "Hourly Rate",
charge: oldBand.timedRate,
price: 0,
cardId: uuidv4()
})
}
if(oldBand.youtube1){
newPosts.push({
type: "video",
data: oldBand.youtube1,
date: new Date(),
postId: uuidv4(),
rockOn: []
})
}
if(oldBand.youtube2){
newPosts.push({
type: "video",
data: oldBand.youtube2,
date: new Date(),
postId: uuidv4(),
rockOn: []
})
}
try {
const response = await fetch(`http://localhost:5000/api/autoquotegenerators/${bandUserId}`, {
method: 'PUT',
headers: {
"Content-Type": "application/json; charset=UTF-8",
},
body: JSON.stringify({
quoteGenerator: newQuoteGenerator,
bandLocation: newLocation,
posts: newPosts
})
})
const responseData = await response.json()
bandCount += 1
console.log(bandCount)
} catch (error) {
console.log(error)
}
}
getAccessToken()

Related

Zapier: Pull Data From Two API EndPoints In One Trigger

​
I am working on a trigger where I need to pull data from two API end points. The first endpoint is a contact from a database that retrieves an email address, then to obtain the details for that contact (email) I need to use another end point. once is /Subscriber and the other is /Subsriber/ {email}/ Properties.
 
I am wondering if I can use a variable to obtain all the data in one trigger, as I have is set up in separate triggers right now.
 
Here is the code for both
Subscriber:
url: 'https://edapi.campaigner.com/v1/Subscribers?PageSize=1',
method: 'GET',
headers: {
'Accept': 'application/json',
'X-API-KEY': bundle.authData.ApiKey
},
params: {
'ApiKey': bundle.authData.ApiKey
}
};
return z.request(options).then((response) => {
response.throwForStatus();
const result = z.JSON.parse(response.content);
result.id = result.Items;
return [result];
});
And Subscriber Properties
const options = {
url: `https://edapi.campaigner.com/v1/Subscribers/${bundle.inputData.email_address}/Properties`,
method: 'GET',
headers: {
'Accept': 'application/json',
'X-API-KEY': bundle.authData.ApiKey
},
params: {
'email_address': bundle.inputData.email_address,
'ApiKey': bundle.authData.ApiKey
}
}
return z.request(options).then((response) => {
response.throwForStatus();
const result = z.JSON.parse(response.content);
result.id = result.CustomFields;
return [result];
});
Any help is appreciated.
​
Yes, definitely possible! Unless your subscriber data actually needs to be a separate trigger (which is unlikely, since you probably just trigger off new contacts), it can just be a function. Try something like:
const subscriberPerform = async (z, bundle) => {
const emailResponse = await z.request({
url: "https://edapi.campaigner.com/v1/Subscribers?PageSize=1",
method: "GET",
headers: {
Accept: "application/json",
"X-API-KEY": bundle.authData.ApiKey, // does this need to be both places?
},
params: {
ApiKey: bundle.authData.ApiKey, // does this need to be both places?
},
});
// requires core version 10+
const email = emailResponse.data.email;
const emailDataResponse = await z.request({
url: `https://edapi.campaigner.com/v1/Subscribers/${email}/Properties`,
method: "GET",
headers: {
Accept: "application/json",
"X-API-KEY": bundle.authData.ApiKey,
},
params: {
email_address: bundle.inputData.email_address, // lots of duplicated data here
ApiKey: bundle.authData.ApiKey,
},
});
return [emailDataResponse.data.SOMETHING];
};
That's the general idea. These JS functions may not need to be triggers at all, depending on how you're using them.
One last note - you don't want to perform this extra lookup every time you poll for new contacts; that's wasteful. If you're doing that, check out dehydration.

Google Cloud Print Node API TICKET not working

This is my code and I am using npm node-gcp and sadly, the ticket is not working, I tried to make the copies = 3 to see if the printer will print 3 and to see if the ticket is working or not, But it's not working :(
I tried all the resources i can find, php ticket, c# ticket, But all are not working,
I am inside the code of npm node-gcp and I am trying to modify its code and see what will work and not, because the original code is he is not passing any ticket.
return preq({
method: "POST",
uri: "https://www.google.com/cloudprint/submit",
form: {
title: "Print job title",
content:
"test print",
ticket: {
version: "1.0",
print: {
copies: {
copies: 3
},
page_orientation: {
type: 0
},
margins: {
top_microns: 0,
bottom_microns: 0,
left_microns: 0,
right_microns: 0
}
}
},
contentType: "url", //optional, default = url
printerid: xxxxxxxxxxxxx,
tags: ["tag1", "tag2"] //optional, default = [],
},
headers: {
"X-CloudPrint-Proxy": "node-gcp",
Authorization: "OAuth " + this.options.accessToken
},
json: true
})
.then(function(result) {
return result;
})
.nodeify(cb);
});
i am expecting a response of number of pages is 3 but all I am getting is 1 piece of paper.
I just had to deal with this issue and discovered that ticket needs to be sent as a stringified json object.
For form, try this instead:
form: {
title: "Print job title",
content:
"test print",
ticket: JSON.stringify({
version: "1.0",
print: {
copies: {
copies: 3
},
page_orientation: {
type: 0
},
margins: {
top_microns: 0,
bottom_microns: 0,
left_microns: 0,
right_microns: 0
}
})
},

Twilio Autopilot: How to pull data from Airtable and return results to user?

I'm using Collect in the first Autopilot task to get the date from the user (i.e. Jan 31, 2019). Then I am trying to use that variable (date) to search an Airtable database for that specific row. I want to return the results of that row to the user ("on Jan 31 the Main Theater is playing this movie, the Other Theater is playing...).
My Airtable Get code is below, but I am unsure how to assign the returned data to variables that I can read back to the user.
exports.handler = function(context, event, callback) {
var Airtable = require('airtable');
var base = new Airtable({apiKey:
'apikey'}).base('basekey');
base('Blockouts').find('recbGHAQopLQdCOHK', function(err, record) {
if (err) { console.error(err); return; }
console.log(record);
});
}
and here's the output that Airtable will send back:
{
"id": "recbGHAQopLQdCOHK",
"fields": {
"Date": "2019-02-02",
"Main Theater": "Star Wars",
"Other Theater": "Avengers"
},
"createdTime": "2019-02-02T21:21:48.000Z"
}
Twilio evangelist here.
If your Airtable Get code is not there already, it should go in a Twilio function (which supports Node.js)! that is pointed at an Autopilot task whose actions code has something like this:
{
"actions": [
{
"redirect": {
"uri": "https://your-twilio-function-path.twil.io/airtable-or-whatever-if-this-is-your-path"
}
}
]
}
Then in your Twilio function/Airtable Get code, you should modify your Twilio Response object to accept all requesting origins and have that look something like this:
exports.handler = function(context, event, callback) {
let response = new Twilio.Response();
let headers = {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
};
var responseObject = {"actions":[
{ "say": "Try again" }
]};
response.setHeaders(headers);
var Airtable = require('airtable');
var base = new Airtable({apiKey: 'apikey'}).base('basekey');
base('Blockouts').find('recbGHAQopLQdCOHK', function(err, record) {
if (err) { console.error(err); return; }
console.log(record);
}).then(res => {
var date = res.data[0].fields.date;
responseObject = {"actions":[
{ "say": date }
]};
callback(null, responseObject);
});
};
I have not tested this code with your Airtable output (famous last words of a programmer), but used almost exactly the same code when making an Axios request to an API where I saved some of the data returned from that call in a variable, so it should be similar.
Hope this helps.

cannot set the Sync document's ttl from a twilio function runtime client

I need my twilio sync documents to be deleted automatically after few minutes.
I followed the documentation for the Runtime.getSync(),
which by the way seems to be introduced here
https://www.twilio.com/docs/runtime/client and extended by the
"Documents" section here
https://www.twilio.com/docs/sync/api/documents using node.js examples,
it was so hard to piece it all toghether.
but I cannot get the ttl parameter to work
exports.handler = function(context, event, callback) {
let sync = Runtime.getSync();
let payload = {
'greeting': "bonbon!"
};
let uniqueName = "test4";
sync.documents.create({
uniqueName: uniqueName,
ttl: 5, //////////////////DOESN'T WORK!!!
data: payload
}).then(function(response) {
console.log(response);
callback(null, response);
});
}
the ttl parameter doesn't cause any effect on the expiration of the document and doesn't even cause the document's parameter "date_expires" (as seen in the https://www.twilio.com/docs/sync/api/documents ecample) to reflect the right value, in fact the date_expires parameter doesn't even appear in the returned JSON object:
{
_version: {
_domain: {
twilio: {
username: "xxxxxxxxxxxxxxxxxxxxxx",
password: "[Redacted]",
accountSid: "xxxxxxxxxxxxxxxxxxxxxx",
httpClient: { },
_sync: {
$ref: "$["_version"]["_domain"]"
}
},
baseUrl: "https://sync.twilio.com",
_v1: {
$ref: "$["_version"]"
}
},
_version: "v1"
},
sid: "xxxxxxxxxxxxxxxxxxxxxx",
uniqueName: "test4",
accountSid: "xxxxxxxxxxxxxxxxxxxxxx",
serviceSid: "xxxxxxxxxxxxxxxxxxxxxx",
url: "https://sync.twilio.com/v1/Services/xxxxxxxxxxxxxxxxxxxxxx/Documents/xxxxxxxxxxxxxxxxxxxxxx",
links: {
permissions: "https://sync.twilio.com/v1/Services/xxxxxxxxxxxxxxxxxxxxxx/Documents/xxxxxxxxxxxxxxxxxxxxxx/Permissions"
},
revision: "0",
data: {
greeting: "bonbon!"
},
dateCreated: "2018-09-19T03:30:24.000Z",
dateUpdated: "2018-09-19T03:30:24.000Z",
createdBy: "system",
_solution: {
serviceSid: "default",
sid: "xxxxxxxxxxxxxxxxxxxxxx"
}
}
is there anyone who has an explanation or a workaround?
Thank you very much in advance
Which version of twilio do you use?
https://www.twilio.com/console/runtime/functions/configure
If it is less than 3.6.10, ttl is not supported.
https://github.com/twilio/twilio-node/blob/master/CHANGES.md#2017-11-17-version-3100
[2017-11-17] Version 3.10.0
Sync
Add TTL support for Sync objects (breaking change)
The newest version is 3.21.0. Try it.

Telegram (simplest) inline bot send photo 2 times on iOS

I have an inline bot similar to #pic and it works ok, when I type query word I can see pictures appear so I can choose one and send to a chat. The problem is when I do - 2 copies of same result are sent. This happens only on iOS client. On Android, PC and other platforms only one picture is being sent. I have checked all logs (the bot is done in Node.js) and for every request I have a single response. It seems to be a iOS client bug, although #pic bot works fine. Has someone encountered this bug or have an idea of what can cause it?
Example of answerInlineQuery response object
{
"inline_query_id": "817150058382989968",
"results": [
{
"type": "photo",
"id": "se090",
"photo_url": "http://www.shadowera.com/secardbot361/se090.jpg",
"thumb_url": "http://www.shadowera.com/secardbot361/se090.jpg",
"photo_width": 344,
"photo_height": 480,
"title": "Tracking Gear",
"description": "You can view the hands of opposing players.",
"caption": "king"
},
{...
UPDATE:
So I have created a simplest possible inline bot in node.js #iosinlinebot (you can try it) AND you have the same exact behaviour: only on iOS devices you will send 2 images to the chat once tapped on the result.
Here is the code:
exports.handler = function(event, context) {
console.log(event);
const https = require("https");
let answer = {
inline_query_id: event.inline_query.id,
results: [{
type: "photo",
id: "abcd",
photo_url: "https://lh3.googleusercontent.com/jVXglyWWL5J2y1vRN-7Jy3_ozvvZc4w5486IAkbAIrWcNN_vn7YuIvhc1JDtGq43BqGl=s180",
thumb_url: "https://lh3.googleusercontent.com/jVXglyWWL5J2y1vRN-7Jy3_ozvvZc4w5486IAkbAIrWcNN_vn7YuIvhc1JDtGq43BqGl=s180",
photo_width: 180,
photo_height: 180,
title: "title",
description: "description",
caption: "test"
}],
cache_time:1
};
let postBody = JSON.stringify(answer);
let options = {
hostname: "api.telegram.org",
port: 443,
path: "/bot" + process.env.TOKEN + "/answerInlineQuery",
method: "POST",
headers: {
'Content-Type': 'application/json',
'Content-Length': postBody.length
}
};
let postreq = https.request(options, (res) => {
res.setEncoding('utf8');
const body = [];
res.on('data', (chunk) => body.push(chunk));
res.on('end', () => {
let j = body.join('');
console.log(j);
//context.done(JSON.parse(j));
});
});
postreq.write(postBody);
postreq.end();
};
this is an event object (coming from telegram):
{
"update_id": 12345678,
"inline_query": {
"id": "123456789123456789",
"from": {
"id": 123456789,
"is_bot": false,
"first_name": "Firstname",
"username": "username",
"language_code": "it-IT"
},
"query": "test",
"offset": ""
}
}
UPDATE:
Thanks to Sedric Heidarizarei we were able to find the problem. It is a telegram iOS client bug. If InlineQueryResultPhoto object contains caption field, you user will post 2 images to the chat.
It is very important to close the Begin and the End of your regex with ^ and $.
For example a user with this regex /^[/]start/ can use start and start a and start b as Bot command And will allow Them to receive your photo, But with /^[/]start$/, The user must enter the exact /start Command.
1: Use This Module: node-telegram-bot-api
2: And Send Your Photo:
bot.onText(/^[/]start$/, (msg) => {
const opts = {
parse_mode: 'Markdown',
reply_markup: {
inline_keyboard: [[{
text: '🔙',
callback_data: 'back'
}]]
}
};
bot.sendPhoto(msg.chat.id, 'AgADBAADn64xBoABCx8L8trMV9eMqgDAAEC', opts); // Your Photo id
});
Notice:
Open an empty project and just use and check your InlineQueryResultPhoto.
update:
That is a Telegram bug for For temporary use, remove caption from your let answer ={}

Resources