2

I get this error when I register a new account. The code I use:

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

exports.afterRegFunction = functions.auth.user().onCreate((user) => {

    var randInt = (Math.floor(Math.random() * 8) + 1);
    var userObject = {
        u_n : user.uid, // user nickname
        u_n_c : admin.database.ServerValue.TIMESTAMP, // next date to change nick
        u_a : randInt.toString(), // user avatar
        u_a_c : admin.database.ServerValue.TIMESTAMP, // next date to change avatar
        d : admin.database.ServerValue.TIMESTAMP, // date the account was created
        u_p : 0 // user points
    };
    admin.database().ref('userInfo/' + user.uid).set(userObject);
    admin.database().ref('nicknames/' + user.uid).set(user.uid);
});

I'm getting Function finished with status: 'error' in the logs and there isn't anything added in the database.

3
  • Boy, uhh... well that's a head scratcher for sure. You're doing it exactly like the documentation suggests. I'd assume if you threw in a console.log(user); as the first line of the function, before the var randInt line, it would just print out null too, but sometimes weird things happen... I'd also try out a console.log(typeof user); and see if that tells us anything either. Commented Dec 27, 2018 at 16:58
  • What version of the firebase-functions package are you using? I notice you're using an old package's way of initializing the app.... admin.initializeApp(functions.config().firebase); was changed to be just admin.initializeApp();... assuming you're on version 1.0 or later, try updating your code to initialize the SDK with the newer admin.initializeApp(); instead Commented Dec 27, 2018 at 17:43
  • "firebase-functions": "^2.1.0" Commented Dec 27, 2018 at 17:55

1 Answer 1

2

You're ignoring the promises returned by set(), which is asynchronous. With Cloud Functions background type functions, you're obliged to return a promise that resolves only after all the async work is complete. Since you have two items of work, you're going to need a new promise that resolves when both of them are resolved.

First of all, you should read the documentation on terminating functions, and watch the video series on how to deal with promises in Cloud Functions so you understand what you need to do.

Then, you'll realize you need to do the following:

const p1 = admin.database().ref('userInfo/' + user.uid).set(userObject);
const p2 = admin.database().ref('nicknames/' + user.uid).set(user.uid);
return Promise.all([p1, p2]);

Also with Cloud Functions, you're supposed to initialize the Admin SDK like this with no arguments:

admin.initializeApp();

The way you're doing it now is deprecated.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the solution and the link!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.