Last active
May 31, 2018 18:26
-
-
Save vikdenic/624f808ec6ba4b54be3c1d87edc252a3 to your computer and use it in GitHub Desktop.
cloud function returning socket error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
// Import Admin SDK | |
var admin = require("firebase-admin"); | |
var reqprom = require('request-promise'); | |
admin.initializeApp(functions.config().firebase); | |
exports.minutely_tick = | |
functions.pubsub.topic('minutely-tick').onPublish((event) => { | |
console.log('This job runs every 5 minutes!'); | |
getAllData() | |
.then((dataArray) => { | |
const ref = admin.database().ref(`myData/`); | |
ref.set(dataArray); | |
return console.log('dataArray length:', dataArray.length); | |
}) | |
.catch((error) => { | |
return console.log('Caught error: ', error); | |
}); | |
}); | |
function getAllData() { | |
return new Promise((resolve, reject) => { | |
var promises = []; | |
var startInt = 0; | |
for (i = 0; i < 1000; i++) { | |
const options = { | |
method: GET, | |
uri: myUrlString, | |
json: true | |
} | |
promises.push(reqprom(options)); | |
startInt += 100; | |
} | |
Promise.all(promises) | |
.then((dict) => { | |
var array = valuesFromDict(dict); | |
return resolve(array); | |
}) | |
.catch((error) => { | |
return reject(error); | |
}); | |
}); | |
} | |
function valuesFromDict(dict) { | |
var values = Object.keys(dict).map((key) => { | |
return dict[key]; | |
}); | |
return values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment