Firebase cloud functions - TypeError: Can not read property '

0

I explain my problem, I have made a function for firebase for when it is inserted, in this case, a new side in Firebase Firestone , the function sends a notification to the users. The problem is when I try to get the id of the document because I need it. I have followed the examples that come in the documentation and as I do I get this error when trying to read the id:

  

TypeError: Can not read property 'side' of undefined       at exports.bandoCreado.functions.firestore.document.onCreate (/user_code/index.js:17:30)       at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)       at next (native)       at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71       at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)       at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)       at /var/tmp/worker/worker.js:700:26       at process._tickDomainCallback (internal / process / next_tick.js: 135: 7)

What comes from this line in particular:

const idBando=event.params.bando;

I leave the full function in case there is still some doubt, a greeting.

 exports.bandoCreado = functions.firestore
.document('bandos/{bando}')
.onCreate((event) =>{
    const idBando=event.params.bando;

    var datos=event.data.data();

    var titulon=datos.titulo;
    var contenidon=datos.contenido;
    var f_empiecen=datos.f_empiece;
    var f_finn=datos.f_fin;
    var t_stampn=datos.t_stamp;

    const payload={
        data: {
            titulo: titulon,
            mensaje: contenidon,
            id: idBando,
            f_empiece: f_empiecen,
            f_fin: f_finn,
            t_stamp: t_stampn
        }
    };

    return admin.messaging().sendToTopic("notificaciones",payload)
        .then(function(response) {
        // See the MessagingTopicResponse reference documentation for the
        // contents of response.
        console.log("Successfully sent message:", response);
        return "";
      })
      .catch(function(error) {
        console.log("Error sending message:", error);
        return "";
      });
});
    
asked by Adrián Garrido Blázquez 09.04.2018 в 19:59
source

1 answer

2

On April 3, the new API version of Cloud Functions was released and there were some changes.

In your particular case, the trigger:

.onCreate((event) =>{});

changed to

.onCreate((userMetadata, context) => {});

And it's now the context argument that has the params property that you're trying to use.

In the following link you can see the changes made to the API with respect to Firestore: link

    
answered by 14.04.2018 / 23:18
source