AngularJS tutorial Thinkster.io chapter 7

泪湿孤枕 提交于 2019-12-25 03:20:42

问题


UPDATE: The tutorial was updated and the following question really no longer applies


Learning about AngularJS from the site thinkster.io (free ebook). But at the moment i'm stuck at chapter 7 - Creating your own user data using firebase. This is an tutorial about angularjs that works with firebase.

I have wrote all the code according to the site, but i'm getting these console errors when I want to register a user. It will create the user (in firebase -simplelogin), but not the user object (in firebase - data).:

TypeError: undefined is not a function
at Object.User.create (http://localhost:9000/scripts/services/user.js:46:19)
at http://localhost:9000/scripts/controllers/auth.js:32:22
etc.

This is the code (same as the site), the error is in the create() function and talks about the users.$save() function, snippet of User.create():

users.$save(username).then(function () {
    setCurrentUser(username);
});

Complete code of user.js:

news.factory("User", function ($firebase, FIREBASE_URL, $rootScope, $log) {
    var reference, users, User;

    reference = new Firebase(FIREBASE_URL + "users");
    users = $firebase(reference);

    function setCurrentUser(username) {
        $rootScope.currentUser = User.findByUsername(username);
    }

    $rootScope.$on("$firebaseSimpleLogin:login", function (event, authUser) {
        var query = $firebase(reference.startAt(authUser.uid).endAt(authUser.uid));

        query.$on("loaded", function () {
            setCurrentUser(query.$getIndex()[0]);
        });
    });

    $rootScope.$on("$firebaseSimpleLogin:logout", function () {
        delete $rootScope.currentUser;
    });

    User = {
        create: function (authUser, username) {
            users[username] = {
                md5_hash: authUser.md5_hash,
                username: username,
                "$priority": authUser.uid
            };

            $log.debug(users);

            users.$save(username).then(function () {
                setCurrentUser(username);
            });
        },
        findByUsername: function (username) {
            if (username) {
                return users.$child(username);
            }
        },
        getCurrent: function () {
            return $rootScope.currentUser;
        },
        signedIn: function () {
            return $rootScope.currentUser !== undefined;
        }
    };

    return User;
});

Edit 1: Registering a user now works, got it working (saving in firebase, simple login and data):

users = $firebase(reference).$asObject();

Notice the users.save() function:

        create: function (authUser, username) {
            users[username] = {
                md5_hash: authUser.md5_hash,
                username: username,
                $priority: authUser.uid
            };

            $log.debug(users);

            users.$save().then(function () {
                setCurrentUser(users);
            });
        },
        findByUsername: function (users) {
            if (users) {
                return users;
            }
        },

Edit 2: Now I get an error at the log in of the user (see below), when I want to log in, I get an error on this this function, query.$on():

TypeError: undefined is not a function
at http://localhost:9000/scripts/services/user.js:26:19
    $rootScope.$on("$firebaseSimpleLogin:login", function (event, authUser) {
        var query = $firebase(reference.startAt(authUser.uid).endAt(authUser.uid));

        query.$on("loaded", function () {
            setCurrentUser(query.$getIndex()[0]);
        });
    });

What is wrong now?


回答1:


This is an answer on edit 2: I have used firebase(ref), query.$loaded and searched for the right object, that's it. Maybe someone have an different answer, please post them :).

I have finally completed chapter 07!

In general (solution for Edit 2):

    $rootScope.$on("$firebaseSimpleLogin:login", function (event, authUser) {
        var query = $firebase(reference).$asObject();

        query.$loaded(function (result) {
            angular.forEach(result, function (key) {
                if (key.md5_hash === authUser.md5_hash) {
                    setCurrentUser(key);
                }
            });
        });
    });

This is not the ideal solution, but the free ebook (atm of writing) is far from ideal. Then again, these kind of situations helps you to understand a little bit more about the firebase api and how it works with angular. But can be frustrated at times, when you just want to go through the tutorial ;).

Note! I have saved the User object and pass the User object to the findUsername() and setCurrentUser() functions instead of just the user.username.

You can also use the native array function, like some().




回答2:


I think your system uses the newer version of Angularfire (version>= 0.8). Which means for running through loops that are arrays ...you need to attach .$asArray() at the end of the user definition field. Check the updates of Firebase.



来源:https://stackoverflow.com/questions/25633141/angularjs-tutorial-thinkster-io-chapter-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!