Reputation: 161
I'm trying to create a scheduled cloud function which updates a document field with a value from an api call. However I get the error mentioned above in the logs, altought I'm returning a promise (as far as I know)
Any help is appretiated
the api call returns this json:
{ "alarm": "false" }
My Code:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as rp from 'request-promise';
admin.initializeApp()
export const scheduledAlarmUpdate = functions.pubsub.schedule('every 30 minutes').onRun((context) => {
const docRef = admin.firestore().collection('alarms').doc('stuttgart');
const username = 'bitfactory';
const pass = '...';
const options = {
url: 'https://api.bitfactory.io/fineparticlesalarm/',
auth: {
user: username,
password: pass
},
method: 'GET',
json: true
}
return rp(options).then(data => {
console.log(data.alarm)
docRef.set({
alarm: data.alarm
})
.catch(err => {
console.log(err);
});
})
});
Upvotes: 0
Views: 193
Reputation: 83191
You need to return the promise returned by the set()
method, as follows:
export const scheduledAlarmUpdate = functions.pubsub
.schedule('every 30 minutes')
.onRun(context => {
const docRef = admin
.firestore()
.collection('alarms')
.doc('stuttgart');
const username = 'bitfactory';
const pass = '...';
const options = {
url: 'https://api.bitfactory.io/fineparticlesalarm/',
auth: {
user: username,
password: pass
},
method: 'GET',
json: true
};
return rp(options)
.then(data => {
console.log(data.alarm);
return docRef.set({ // ->>> here return
alarm: data.alarm
});
})
.catch(err => {
console.log(err);
});
});
Upvotes: 1