0

The function worke perfectly, but if I want to print the content of the user, I receive in the firebase log this info:

Function returned undefined, expected Promise or value

The function is:

exports.accountCreate = functions.auth.user().onCreate(user => {
console.log("--->"+user.data);
console.log("ok");
});

Why the user.data is not able to retrieved the informations?

Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jonio
  • 1,213
  • 2
  • 15
  • 35
  • 1
    You must return a value, to indicate to the Cloud Functions container when your function is done. In your case that can be a simple `return true` at the end. See https://stackoverflow.com/questions/47128440/google-firebase-errorfunction-returned-undefined-expected-promise-or-value. – Frank van Puffelen Jan 10 '19 at 14:50

1 Answers1

2

Cloud Functions run in a container in a managed environment. The environment tries to minimize how long it keeps the container running, and to be able to do so, it must know when your function is done. Normally in JavaScript code is done when the last } has executed, but this gets more complex when you also need to consider asynchronous operations. For this reason Cloud Functions expects you to inform it when the function is done, in the case of functions.auth.user().onCreate by returning a value or promise.

When you explicitly return a value, it is clear that the function is done. When you explicitly return a promise, it's clear that the function needs to remain active until the promise is resolved/rejected. When you don't return a value, it is not clear what state the function is in.

In your case the fix is simple, and you for example just return true before the final }.

exports.accountCreate = functions.auth.user().onCreate(user => {
  console.log("--->"+user.data);
  console.log("ok");
  return true;
});

The actual value is meaningless btw, a return null would work just as well.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks, I solve the problem, but I find that the user.data give me result like undefinied... I do know the why... – Jonio Jan 10 '19 at 18:09