Firebase user authentication trigger that writes to realtime database
exports.setupDefaultPullups = functions.auth.user()
.onCreate(
async (user) => {
const dbRef= functions.database.ref;
let vl= await (dbRef.once('value').then( (snapshot) => {
return snapshot.ref.child('userInfo/'+user.uid).set(18);
}));
return vl;
}
);
I am trying to write a trigger for some initial set-up for a new user, in Firebase. However, the above code does not work. What is wrong with it?
Basically, upon registering a new user, I would like to set up his/her property "defaultPullUps" to, say, 18, using the above path.
EDIT: I am sorry for not giving details. Initially, there was an issue with the "async" keyword, that has been fixed by updating the node.js engine. Now, I get various error messages depending on how I tweak the code. Sometimes it says "... is not a function".
EDIT': Although I agree my question is not up to par, but there is a value in it: in online documentation of Firebase authentication triggers, how to access the "main" database is not shown https://firebase.google.com/docs/functions/auth-events
EDIT'': here is the entire message:
TypeError: Cannot read property 'child' of undefined
at exports.setupDefaultPullups.functions.auth.user.onCreate.user (/srv/index.js:15:36)
at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at /worker/worker.js:756:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
javascript firebase firebase-realtime-database google-cloud-functions
|
show 1 more comment
exports.setupDefaultPullups = functions.auth.user()
.onCreate(
async (user) => {
const dbRef= functions.database.ref;
let vl= await (dbRef.once('value').then( (snapshot) => {
return snapshot.ref.child('userInfo/'+user.uid).set(18);
}));
return vl;
}
);
I am trying to write a trigger for some initial set-up for a new user, in Firebase. However, the above code does not work. What is wrong with it?
Basically, upon registering a new user, I would like to set up his/her property "defaultPullUps" to, say, 18, using the above path.
EDIT: I am sorry for not giving details. Initially, there was an issue with the "async" keyword, that has been fixed by updating the node.js engine. Now, I get various error messages depending on how I tweak the code. Sometimes it says "... is not a function".
EDIT': Although I agree my question is not up to par, but there is a value in it: in online documentation of Firebase authentication triggers, how to access the "main" database is not shown https://firebase.google.com/docs/functions/auth-events
EDIT'': here is the entire message:
TypeError: Cannot read property 'child' of undefined
at exports.setupDefaultPullups.functions.auth.user.onCreate.user (/srv/index.js:15:36)
at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at /worker/worker.js:756:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
javascript firebase firebase-realtime-database google-cloud-functions
1
You asked this question before assuming that there was a problem with the async keyword. This time, you still haven't really helped us understand what the problem is. Is there an error message? What exactly are you observing that's different than what you expect?
– Doug Stevenson
Nov 14 '18 at 4:55
It's expected that you give the entire error message that gives some indication of the problem. Please don't edit down the message if you don't understand it.
– Doug Stevenson
Nov 14 '18 at 5:02
I've done it using Java inside the app, but still it is interesting to see how to write the logic server-side
– Ilonpilaaja
Nov 14 '18 at 5:06
You're not using Java in Cloud Functions. You're using JavaScript. They are actually very different languages. Don't assume the APIs are the same.
– Doug Stevenson
Nov 14 '18 at 5:07
Your error message doesn't seem to match the code you're showing. Are you certain that the code here is the same as what you deployed to Cloud Functions?
– Doug Stevenson
Nov 14 '18 at 5:12
|
show 1 more comment
exports.setupDefaultPullups = functions.auth.user()
.onCreate(
async (user) => {
const dbRef= functions.database.ref;
let vl= await (dbRef.once('value').then( (snapshot) => {
return snapshot.ref.child('userInfo/'+user.uid).set(18);
}));
return vl;
}
);
I am trying to write a trigger for some initial set-up for a new user, in Firebase. However, the above code does not work. What is wrong with it?
Basically, upon registering a new user, I would like to set up his/her property "defaultPullUps" to, say, 18, using the above path.
EDIT: I am sorry for not giving details. Initially, there was an issue with the "async" keyword, that has been fixed by updating the node.js engine. Now, I get various error messages depending on how I tweak the code. Sometimes it says "... is not a function".
EDIT': Although I agree my question is not up to par, but there is a value in it: in online documentation of Firebase authentication triggers, how to access the "main" database is not shown https://firebase.google.com/docs/functions/auth-events
EDIT'': here is the entire message:
TypeError: Cannot read property 'child' of undefined
at exports.setupDefaultPullups.functions.auth.user.onCreate.user (/srv/index.js:15:36)
at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at /worker/worker.js:756:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
javascript firebase firebase-realtime-database google-cloud-functions
exports.setupDefaultPullups = functions.auth.user()
.onCreate(
async (user) => {
const dbRef= functions.database.ref;
let vl= await (dbRef.once('value').then( (snapshot) => {
return snapshot.ref.child('userInfo/'+user.uid).set(18);
}));
return vl;
}
);
I am trying to write a trigger for some initial set-up for a new user, in Firebase. However, the above code does not work. What is wrong with it?
Basically, upon registering a new user, I would like to set up his/her property "defaultPullUps" to, say, 18, using the above path.
EDIT: I am sorry for not giving details. Initially, there was an issue with the "async" keyword, that has been fixed by updating the node.js engine. Now, I get various error messages depending on how I tweak the code. Sometimes it says "... is not a function".
EDIT': Although I agree my question is not up to par, but there is a value in it: in online documentation of Firebase authentication triggers, how to access the "main" database is not shown https://firebase.google.com/docs/functions/auth-events
EDIT'': here is the entire message:
TypeError: Cannot read property 'child' of undefined
at exports.setupDefaultPullups.functions.auth.user.onCreate.user (/srv/index.js:15:36)
at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at /worker/worker.js:756:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
javascript firebase firebase-realtime-database google-cloud-functions
javascript firebase firebase-realtime-database google-cloud-functions
edited Nov 14 '18 at 5:38
Ilonpilaaja
asked Nov 14 '18 at 4:52
IlonpilaajaIlonpilaaja
317214
317214
1
You asked this question before assuming that there was a problem with the async keyword. This time, you still haven't really helped us understand what the problem is. Is there an error message? What exactly are you observing that's different than what you expect?
– Doug Stevenson
Nov 14 '18 at 4:55
It's expected that you give the entire error message that gives some indication of the problem. Please don't edit down the message if you don't understand it.
– Doug Stevenson
Nov 14 '18 at 5:02
I've done it using Java inside the app, but still it is interesting to see how to write the logic server-side
– Ilonpilaaja
Nov 14 '18 at 5:06
You're not using Java in Cloud Functions. You're using JavaScript. They are actually very different languages. Don't assume the APIs are the same.
– Doug Stevenson
Nov 14 '18 at 5:07
Your error message doesn't seem to match the code you're showing. Are you certain that the code here is the same as what you deployed to Cloud Functions?
– Doug Stevenson
Nov 14 '18 at 5:12
|
show 1 more comment
1
You asked this question before assuming that there was a problem with the async keyword. This time, you still haven't really helped us understand what the problem is. Is there an error message? What exactly are you observing that's different than what you expect?
– Doug Stevenson
Nov 14 '18 at 4:55
It's expected that you give the entire error message that gives some indication of the problem. Please don't edit down the message if you don't understand it.
– Doug Stevenson
Nov 14 '18 at 5:02
I've done it using Java inside the app, but still it is interesting to see how to write the logic server-side
– Ilonpilaaja
Nov 14 '18 at 5:06
You're not using Java in Cloud Functions. You're using JavaScript. They are actually very different languages. Don't assume the APIs are the same.
– Doug Stevenson
Nov 14 '18 at 5:07
Your error message doesn't seem to match the code you're showing. Are you certain that the code here is the same as what you deployed to Cloud Functions?
– Doug Stevenson
Nov 14 '18 at 5:12
1
1
You asked this question before assuming that there was a problem with the async keyword. This time, you still haven't really helped us understand what the problem is. Is there an error message? What exactly are you observing that's different than what you expect?
– Doug Stevenson
Nov 14 '18 at 4:55
You asked this question before assuming that there was a problem with the async keyword. This time, you still haven't really helped us understand what the problem is. Is there an error message? What exactly are you observing that's different than what you expect?
– Doug Stevenson
Nov 14 '18 at 4:55
It's expected that you give the entire error message that gives some indication of the problem. Please don't edit down the message if you don't understand it.
– Doug Stevenson
Nov 14 '18 at 5:02
It's expected that you give the entire error message that gives some indication of the problem. Please don't edit down the message if you don't understand it.
– Doug Stevenson
Nov 14 '18 at 5:02
I've done it using Java inside the app, but still it is interesting to see how to write the logic server-side
– Ilonpilaaja
Nov 14 '18 at 5:06
I've done it using Java inside the app, but still it is interesting to see how to write the logic server-side
– Ilonpilaaja
Nov 14 '18 at 5:06
You're not using Java in Cloud Functions. You're using JavaScript. They are actually very different languages. Don't assume the APIs are the same.
– Doug Stevenson
Nov 14 '18 at 5:07
You're not using Java in Cloud Functions. You're using JavaScript. They are actually very different languages. Don't assume the APIs are the same.
– Doug Stevenson
Nov 14 '18 at 5:07
Your error message doesn't seem to match the code you're showing. Are you certain that the code here is the same as what you deployed to Cloud Functions?
– Doug Stevenson
Nov 14 '18 at 5:12
Your error message doesn't seem to match the code you're showing. Are you certain that the code here is the same as what you deployed to Cloud Functions?
– Doug Stevenson
Nov 14 '18 at 5:12
|
show 1 more comment
1 Answer
1
active
oldest
votes
This line makes no sense:
const dbRef= functions.database.ref;
If you want to use the Firebase Realtime Database in a Cloud Function that is triggered from another source (such as Firebase Authentication in your case), you can do so by using the Firebase Admin SDK.
For an example of this, see the Initialize Firebase SDK for Cloud Functions section of the Get started with Cloud Functions documentation. Specifically, you'll need to import and initialize is using:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
And then can access the database from your function using:
const dbRef= admin.database().ref();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293371%2ffirebase-user-authentication-trigger-that-writes-to-realtime-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This line makes no sense:
const dbRef= functions.database.ref;
If you want to use the Firebase Realtime Database in a Cloud Function that is triggered from another source (such as Firebase Authentication in your case), you can do so by using the Firebase Admin SDK.
For an example of this, see the Initialize Firebase SDK for Cloud Functions section of the Get started with Cloud Functions documentation. Specifically, you'll need to import and initialize is using:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
And then can access the database from your function using:
const dbRef= admin.database().ref();
add a comment |
This line makes no sense:
const dbRef= functions.database.ref;
If you want to use the Firebase Realtime Database in a Cloud Function that is triggered from another source (such as Firebase Authentication in your case), you can do so by using the Firebase Admin SDK.
For an example of this, see the Initialize Firebase SDK for Cloud Functions section of the Get started with Cloud Functions documentation. Specifically, you'll need to import and initialize is using:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
And then can access the database from your function using:
const dbRef= admin.database().ref();
add a comment |
This line makes no sense:
const dbRef= functions.database.ref;
If you want to use the Firebase Realtime Database in a Cloud Function that is triggered from another source (such as Firebase Authentication in your case), you can do so by using the Firebase Admin SDK.
For an example of this, see the Initialize Firebase SDK for Cloud Functions section of the Get started with Cloud Functions documentation. Specifically, you'll need to import and initialize is using:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
And then can access the database from your function using:
const dbRef= admin.database().ref();
This line makes no sense:
const dbRef= functions.database.ref;
If you want to use the Firebase Realtime Database in a Cloud Function that is triggered from another source (such as Firebase Authentication in your case), you can do so by using the Firebase Admin SDK.
For an example of this, see the Initialize Firebase SDK for Cloud Functions section of the Get started with Cloud Functions documentation. Specifically, you'll need to import and initialize is using:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
And then can access the database from your function using:
const dbRef= admin.database().ref();
answered Nov 14 '18 at 14:51
Frank van PuffelenFrank van Puffelen
232k29380406
232k29380406
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293371%2ffirebase-user-authentication-trigger-that-writes-to-realtime-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You asked this question before assuming that there was a problem with the async keyword. This time, you still haven't really helped us understand what the problem is. Is there an error message? What exactly are you observing that's different than what you expect?
– Doug Stevenson
Nov 14 '18 at 4:55
It's expected that you give the entire error message that gives some indication of the problem. Please don't edit down the message if you don't understand it.
– Doug Stevenson
Nov 14 '18 at 5:02
I've done it using Java inside the app, but still it is interesting to see how to write the logic server-side
– Ilonpilaaja
Nov 14 '18 at 5:06
You're not using Java in Cloud Functions. You're using JavaScript. They are actually very different languages. Don't assume the APIs are the same.
– Doug Stevenson
Nov 14 '18 at 5:07
Your error message doesn't seem to match the code you're showing. Are you certain that the code here is the same as what you deployed to Cloud Functions?
– Doug Stevenson
Nov 14 '18 at 5:12