AngularJS教程Thinkster.io第7章 [英] AngularJS tutorial Thinkster.io chapter 7

查看:133
本文介绍了AngularJS教程Thinkster.io第7章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习一下从网站thinkster.io(免费电子书)AngularJS。但此刻我被困在第7章 - 使用火力创建自己的用户数据。这是关于与火力的作品angularjs的教程。

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.

我已经根据现场写的所有code,但我发现这些控制台错误,当我想注册一个用户。它将创建用户(在火力-simplelogin),而不是用户的对象(在火力 - 数据):

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.

这是code(相同点),错误的是在创建()函数和对用户的会谈$保存()函数,User.create()的片段:

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);
});

user.js的中的完整code:

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;
});

编辑1:
注册用户现在工作,得到它的工作(在火力节约,简单的登录和数据):

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

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

注意users.save()函数:

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;
            }
        },

编辑2:
现在,我得到一个错误在日志中的用户(见下文),当我想登录,我得到这个这个功能,查询$上(错误):

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?

推荐答案

这是编辑2回答:我已经使用火力点(REF),查询$加载并寻找合适的对象,仅此而已。也许有人有不同的回答,请张贴。)

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 :).

我终于完成了第07章!

I have finally completed chapter 07!

在一般(编辑2溶液):

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);
                }
            });
        });
    });

这是不理想的解决方案,但是免费的电子书(写作ATM)很不理想。话又说回来,这几样的情况下可以帮助你了解多一点有关火力API和它是如何工作具有角。但有时,当你只是想通过本教程受挫。)

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 ;).

请注意!我已经保存在用户对象和用户对象传递给findUsername()和setCurrentUser()函数,而不是只是user.username的。

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().

这篇关于AngularJS教程Thinkster.io第7章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆