1

I'm new to cloud functions and firestore and I got an exercise in which I have to update a field in a firestore-document called "Stuttgart" in a "alarms" collection. Whenever the alarm field gets changed, the cloud function should trigger, make a call to the API and receive the current alarm status. Then the function should update the alarm field in a Firestore with the received boolean from the API call.

I tried some code below but I'm not getting any further with it so any help is appreciated

firestore looks like this: alarms (collection) -> stuttgart (document) -> alarm: false (field)

The API call gives a JSON object: {"alarm":false,"infos":[]}

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as rp from 'request-promise';

admin.initializeApp()
const db = admin.firestore();

export const onAlarmsUpdate = functions.firestore.document('alarms/{alarmId}')
    .onUpdate((change, context) => {

        const options = {
            url: 'h​ttps://api.bitfactory.io/fineparticlesalarm/',
            method: 'GET',
            auth: {
                'user': 'bitfactory',
                'pass': '...'
            },
            json: true
        }

        return rp(options).then(data => {
            console.log(data.alarm)

            db.collection('alarms').doc('stuttgart').set({
                alarm: data.alarm
            })
            .catch(error => {
                console.log('Error writing document: ' + error);
                return false;
            });
        });
});
Japhet
  • 161
  • 1
  • 2
  • 12
  • See also https://stackoverflow.com/a/54113375/3371862 – Renaud Tarnec May 06 '19 at 12:39
  • @RenaudTarnec is Basic Authentication also integrated in request-promise – Japhet May 06 '19 at 12:45
  • Actually I don't know. – Renaud Tarnec May 06 '19 at 12:48
  • @RenaudTarnec I looked at stackoverflow.com/a/54113375/3371862 and copied the request-promise with the options object. But Firebase says "invalid URI", do you know what could be the problem there? – Japhet May 06 '19 at 21:17
  • Note that if you plan to call a non Google-owned service you need to be on the "Flame" or "Blaze" pricing plan. As a matter of fact, the free "Spark" plan "allows outbound network requests only to Google-owned services". See https://firebase.google.com/pricing/ (hover your mouse on the question mark situated after the "Cloud Functions" title) – Renaud Tarnec May 07 '19 at 04:53
  • @RenaudTarnec my Project runs under the Blaze Plan so that shouldn't be the problem. I updated the code in the original question, cause I really dont know what the problem is. – Japhet May 07 '19 at 06:25
  • What do you get with `console.log(data.alarm)`? The expected values? – Renaud Tarnec May 07 '19 at 08:29
  • @RenaudTarnec I'm not getting there. I tried to test it using the functions:shell but it says "ReferenceError: onAlarmsUpdate is not defined" – Japhet May 07 '19 at 08:53
  • Then your problem comes form another reason. You said initially that "Firebase says "invalid URI". You may try to deploy it. – Renaud Tarnec May 07 '19 at 09:19
  • @RenaudTarnec I can deploy it without any problems. When I check the logs after it says "RequestError: Error: Invalid URI "h​ttps://api.bitfactory.io/fineparticlesalarm/"". I tried using authentication in the request header but it still wont work – Japhet May 07 '19 at 09:36

0 Answers0