IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

Handle Documents in Store #44

Closed MichZipp closed 6 years ago

MichZipp commented 6 years ago

Hi,

I'm a beginner with react and firebase! I would like to have the current User in the store to access the data from different components. If I use the following Code, the currentUser Document is always empty. The "setCurrentUser" function is called, when a user has logged in. If I query the whole user collection with the user id, it works fine. But I think it would be more effitient to directly access the document.

class User extends Document {
    constructor(source, options) {
        super(source, {
            ...(options || {}),
            schema: struct({
                firstname: "string",
                lastname: "string",
                email: "string"
            })
        });
    }
}

var users = new Collection("users", {
    DocumentClass: User
});

var currentUser = new Document();

function setCurrentUser(uid) {
    currentUser = new Document("users/" + uid, {
        DocumentClass: User
    });
} 

export {
    users,
    currentUser,
    setCurrentUser
}

Greetings, Michael

IjzerenHein commented 6 years ago

Hi Michael. What about:

...
var currentUser = new Document();

function setCurrentUser(uid) {
    currentUser.path = "users/" + uid;
}
...

I think that will work.

regards, Hein

MichZipp commented 6 years ago

That's it, Thanks!

gregoryanm commented 5 years ago

Where did you place this in your code, I am placing it on the auth state changed, and nothing so far