Saturday 20 May 2017

There is no user record corresponding to the provided identifier

This function sends a notification to users who installed the application x day ago, retrieving the day of the creation of their account and their fcm token is saved in the database. Since the function is not yet fully functional, it is normal that I do not use the user token at this time. I have this mistake:

Error: There is no user record corresponding to the provided identifier.
    at FirebaseAuthError.Error (native)
    at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
    at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:113:15
    at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:332:13
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

I can't find where it can come from. As you can see I tried a lot of things to catch the error but nothing is working ...

 'use strict';

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);

    exports.sendNotificationToNewUser = functions.https.onRequest((request, response) => {

        Date.prototype.sameDay = function (d) {
            return this.getFullYear() === d.getFullYear()
            && this.getDate() === d.getDate()
            && this.getMonth() === d.getMonth();
        }

        // Loop through users in order with the forEach() method. The callback
        // provided to forEach() will be called synchronously with a DataSnapshot
        // for each child:
        var query = admin.database().ref("users").orderByKey();
        var defaultAuth = admin.auth();
        const getUsersPromise = query.once("value")

        return Promise.all([getUsersPromise]).then(results => {

            const tokensSnapshot = "test";

            results[0].forEach(function (childSnapshot) {
                if (childSnapshot != null) {
                    try {
                        // user will be "ada" the first time and "alan" the second time
                        var user_id = childSnapshot.key;
                        // childData will be the actual contents of the child
                        var user_data = childSnapshot.val();


                        admin.auth().getUser(user_id)
                        .then(function (userRecord) {
                            if (userRecord != null) {
                                var create_date = userRecord.metadata.createdAt
                                var date_now = new Date(Date.now());
                                console.log("Creation date:", create_date);
                                create_date.setDate(create_date.getDate() + 4);
                                if (create_date.sameDay(date_now)) {
                                    //todo something here
                                }
                            }
                        })
                        .catch(function(error) {
                            // Handle Errors here.
                            var errorCode = error.code;
                            var errorMessage = error.message;
                            console.log('User did not sign up correctly');
                            console.log(errorCode);
                            console.console.log(errorMessage);
                        });
                    }
                    catch (e) {
                        console.log(e);
                    }
                }
            });
            // Notification details.
            const payload = {
                notification: {
                    body: "blabla",
                    sound: "default"
                },
                "data": {
                    "key": "CODE",
                    "value": "playstore"
                }
            };

            var options = {
                priority: "high",
                collapseKey: "playstore"
            };

            // Listing all tokens, error here below.
            //const tokens = Object.keys(tokensSnapshot.val());
            const tokens = "my token for the test";

            // Send notifications to all tokens.
            // Send a message to the device corresponding to the provided
            // registration token with the provided options.
            return admin.messaging().sendToDevice(tokens, payload, options).then(response => {
                // For each message check if there was an error.
                const tokensToRemove = [];
                response.results.forEach((result, index) => {
                    const error = result.error;
                    if (error) {
                        console.error('Failure sending notification to', tokens[index], error);
                        // Cleanup the tokens who are not registered anymore.
                        if (error.code === 'messaging/invalid-registration-token' ||
                        error.code === 'messaging/registration-token-not-registered') {
                            tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
                        }
                    }
                    else {
                        console.log("Successfully sent message:", response);
                    }
                });
                return Promise.all(tokensToRemove);
            })
        })
    })



via filol

No comments:

Post a Comment