1

I'm trying to create a todo list with Firebase. After a user authenticates with Google, the user can create tasks, but I can't figure out how to store the task to the user.

I'm trying to use .child(), but I can't know what the uid is for each user is. I also tried .orderByChild(), but that doesn't seem to do it , either (or I'm not using it right).

I've tried:

var firebase = new Firebase('https://<my-app>.firebaseio.com');

firebase.child('users').child(authData.uid).child('task').push({
            status: 'In Progress',
            taskName: $taskName.val(),
            taskDescription: $taskDescription.val(),
            taskCategory: $taskCategory.val()
        })

authData.uid is given by Google on authentication:

firebase.authWithOAuthPopup('google', function(error, authData) {
                if (error) {
                    console.log("Login Failed!", error);
                } else {
                    // Stores user in Firebase
                    firebase.onAuth(function(authData) {
                        if (authData) {
firebase.child('users').child(authData.uid).set({
                                name: authData.google.displayName
                            });
                        }
                    });
                }
            });
2
  • Edit your question to include the minimal-but-complete code that shows the problem please. There is too much room for interpretation with the tiny inline snippets. If you find it difficult to isolate the problem in such a way, try reproducing it in a jsfiddle/jsbin first and add the link and crucial code to your question. See MCVE. Commented Apr 8, 2016 at 4:20
  • Your snippet seems perfectly fine. What is the problem? What would you like to do that doesn't work ? firebase.child('users').child(authData.uid).child('task').push(...) would create a new node under users/$uid/task. Is it not what you want ? Commented Apr 8, 2016 at 14:00

1 Answer 1

1

When adding the task, you build the path like this:

firebase.child('users').child(authData.uid).child('task').push({

That won't work, because authData is not available anymore at this point.

Instead you can get the current auth data by calling ref.getAuth() and do:

var uid = firebase.getAuth().uid;
firebase.child('users').child(uid).child('task').push({

Note that this is covered in the guide on Firebase Authentication and I highly recommend reading it.

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

1 Comment

That was it! Thank you, Frank!

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.