2

This is my code

const functions = require('firebase-functions');

exports.doSomething = functions.auth.user().onCreate(function(user) {
    const userJsonString = JSON.stringify(user.toJSON);
    // doing some network stuff in here the important part is I need a properly working user Object (needs a toJSON function)
});

But how can I trigger the onCreate() function? I already read about using the firebase cli by using the following command firebase functions:shell but when I just trigger the function like this (as mentioned here):

doSomething({uid : "akdfkaf", username : "This is a username"})

It throws an error since toJSON isn't defined on this object.

I also read this on the firebase docs but how do I achieve "following the data format"? Does this mean I have to implement my own classes and implement every function on my own (and probably guess the implementation of things like toJSON() functions)?

As you probably realised I'm completely new to javascript and only need it for the cloud functions since this part only works with javascript.

Hard_Veur
  • 401
  • 1
  • 4
  • 12
  • Since `onCreate` delivers a [UserRecord](https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord.html) object, have you tried creating and sending one to doSomething instead of a plain JS object? You would have to implement its methods yourself, or at least the ones you intend to call. – Doug Stevenson Dec 10 '20 at 01:46
  • What do you mean by `onCreate`? Is there a top level function `onCreate`? If you mean `admin.auth().createUser({username : "username1", ...})` I tried using this function to create a user object (or maybe it would even trigger my cloud function directly) but the problem was that the the firebase cli couldn't find a project id and throw this error `UnhandledPromiseRejectionWarning: Error: Failed to determine project ID: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND` – Hard_Veur Dec 10 '20 at 02:04
  • I'm saying that since the `onCreate` callback function you're showing accepts a UserRecord object, you should try invoking your test function with one that you build (not the plain JS object you're using now). So, `doSomething(userRecordYouImplemented)`. – Doug Stevenson Dec 10 '20 at 02:09
  • Yeah the UserRecord is normally generated by firebase. Every time a new user signs up via the app firebase triggers the function and passes a UserRecord (which firebase created internally) to the this function. To test it I would like to somehow create an exakt same UserRecord as firebase would do it since otherwise this test wouldn't make much sense when in production completely different UserRecords are passed into it. – Hard_Veur Dec 10 '20 at 02:15
  • You can certainly build a UserRecord object that meets your own needs and pass it along to yourself. That's how the CLI tests work - you pass yourself the inputs you want to test with. This is covered in the [documentation](https://firebase.google.com/docs/functions/local-shell). It's up to you to make sure the object has all the properties and methods you intend to use. – Doug Stevenson Dec 10 '20 at 02:30
  • Ok but isn't there any other way? This seems pretty unsafe as I already mentioned. This would mean I have to guess what happens inside the `toJson()` function and I don't wanna even think about how long this would take if I would have more complex objects with probably 10 methods or more. I would need days just to reconstruct the class and to make tests. Seems not pretty good. – Hard_Veur Dec 10 '20 at 02:34
  • No, since you provide the toJSON method, you know exactly what it's going to do. There is no guessing, just testing that the function does what you expect. – Doug Stevenson Dec 10 '20 at 02:37
  • In the test case but normally it's provided by firebase since I never programmed a UserRecord class for production. I would only need to do this for testing. This means when I use it in production I don't know what toJson would look like since UserRecord is implemented by firebase-admin module or am I getting something wrong? – Hard_Veur Dec 10 '20 at 02:40
  • If you want to know what it will do in production, then run it in production. The whole point of the shell test is to take control of every aspect of the inputs so you can specify exactly what you want. If you want to simulate what you think production will do, that's up to you to implement. – Doug Stevenson Dec 10 '20 at 02:42
  • Yeah I want to know how it behaves in production. To figure this out I need to know how toJSON behaves in production. I can't run it in production because of several obvious reasons like losing money, breaking the signing up process,... This is why programmers write tests to don't mess with the actual production environment. – Hard_Veur Dec 10 '20 at 02:49
  • You are well within the free monthly limits to run a function once to see how it works. You might want to visit the pricing page to remind yourself how much you get for free per month. – Doug Stevenson Dec 10 '20 at 03:09

0 Answers0