类型错误:未定义不是一个函数在提交&安培;删除使用AngularFire和AngularJS(ThinksterIO教程第7章)后 [英] TypeError: undefined is not a function while submitting & deleting post using AngularFire and AngularJS (ThinksterIO Tutorial Chapter 7)

查看:150
本文介绍了类型错误:未定义不是一个函数在提交&安培;删除使用AngularFire和AngularJS(ThinksterIO教程第7章)后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到与Thinkster.io AngularJS教程多TypeErrors:学习如何构建现代web应用的第7章使用火力因为AngularFire升级至v0.8.0之后教程现在已经过时创建自己的用户数据。特别是 $子()。在$()已取出的方法。下面是更改日志

https://github.com/firebase/angularfire/releases/tag/ v0.8.0

在<一个href=\"http://stackoverflow.com/questions/25667143/typeerror-undefined-is-not-a-function-with-firebase-and-angularjs-thinksterio\">getting帮助注册新用户在解决我最初的类型错误问题,我开始看到2个新的TypeErrors显示当我提交后,当我试图删除帖子。再一次的罪魁祸首是过时的 $子() $的()方法。

提交帖子>类型错误:未定义不是一个函数

提交后我看到下面的类型错误在控制台中显示出来后

这类型错误指向 post.js 服务

 类型错误:未定义不是一个函数
    在http://本地主机:9000 /脚本/服务/ post.js:16:11

这是在 $子方法调用的开头线16条,第11栏。此外,请注意第二个&安培;子()后的第一个。这将导致在另一类型错误

 返回岗位。$添加(后)。然后(功能(REF){
  变种帖子ID = ref.name();
  。用户$子('职位')$子(帖子ID)$集(帖子ID)。
  返回帖子ID;
});

我会提到,即使我得到这个类型错误,我仍然能够成功创建后,我看到它在锻造和应用程序的文章列表。

删除帖子>类型错误:未定义不是一个函数

当我尝试删除,我刚刚创建我得到有关现在去precated 其他类型错误后,$的()方法。

 类型错误:未定义不是一个函数
    在Object.Post [如删除](HTTP://本地主机:9000 /脚本/服务/ post.js:27:10)
    在范围$ scope.deletePost。(HTTP://本地主机:9000 /脚本/控制器/ posts.js:10:14)

,它指向。在$()在此code在 post.js 服务

 删除:功能(帖子ID){
  如果(User.signedIn()){
    无功后= Post.find(帖子ID);
    帖子。在$('装',函数(){
      VAR用户= User.findByUsername(post.owner);
      帖子。$删除(帖子ID)。然后(函数(){
        。用户$子('职位')$删除(帖子ID);
      });
    });
  }
}

我意识到我将不得不做一些像

 后。$加载(功能(结果){第7章创建使用火力自己的用户数据
  //相关的东西寻找职位的用户
  //删除帖子收集后
  //但我们当然不能用。$子()
});

但问题是,当一个JS初学者和我一样走来面对AngularFire更改日志和API,我只是迷路,不知所措到关停的地步。任何指导,大大AP preciated一如既往。


文件


post.js 服务

 使用严格的;app.factory('邮政',函数($火力点,FIREBASE_URL,用户){
  VAR REF =新的火力地堡(FIREBASE_URL +'职位');
  VAR帖= $火力点(REF)$ asArray()。  VAR邮政= {
    所有:帖子,
    创建:函数(后){
      如果(User.signedIn()){
        变种用户= User.getCurrent();
        post.owner = user.username;        返回岗位。$添加(后)。然后(功能(REF){
          变种帖子ID = ref.name();
          。用户$子('职位')$子(帖子ID)$集(帖子ID)。
          返回帖子ID;
        });
      }
    },
    发现:函数(帖子ID){
            返回$火力(ref.child(帖子ID))$ asObject()。
    },
    删除:功能(帖子ID){
      如果(User.signedIn()){
        无功后= Post.find(帖子ID);
        帖子。在$('装',函数(){
          VAR用户= User.findByUsername(post.owner);
          帖子。$删除(帖子ID)。然后(函数(){
            。用户$子('职位')$删除(帖子ID);
          });
        });
      }
    }
  };  返回帖子;});


post.js 控制器

 使用严格的;app.controller('PostsCtrl',函数($范围,$位置,邮政){  $ scope.posts = Post.all;  $ scope.post = {URL:HTTP://,标题:''};  $ scope.deletePost =功能(POST){
    Post.delete(岗位);
  };});


解决方案

我受够了,所以我把一个简单的方法(只是为了获得槽教程)。我创建userRef(这应该是来自用户对象IMO):

  modulename.factory(邮报,函数($火力点,FIREBASE_URL,用户){
    //其余code    VAR userRef =新的火力地堡(FIREBASE_URL +用户);    VAR邮政= {
        创建:函数(){
            //将code休息            返回岗位。$添加(后)。然后(功能(REF){
                变种帖子ID = ref.name();                userRef.child(用户的$ id).child(上岗)子(帖子ID).SET(帖子ID)。                返回帖子ID;
            });
        }
    }
});

这仅仅是一种方法,当我正在寻找一个好的解决方案,我看到一些例如codeS。我希望它有助于一点点。

I've run into multiple TypeErrors with the Thinkster.io AngularJS Tutorial: Learn to build Modern Webapps Chapter 7. Creating your own user data using firebase since the tutorial is now outdated after AngularFire was upgraded to v0.8.0. Specifically the .$child() and .$on() methods have been taken out. Here is the change log

https://github.com/firebase/angularfire/releases/tag/v0.8.0

After getting help solving my initial TypeError issues during registering a new user, I began to see 2 new TypeErrors show up when I submit a post and when I try to delete a post. Once again the culprits are the outdated .$child() and .$on() methods.

Submitting a Post > TypeError: undefined is not a function

After submitting a post I see the following TypeError show up in the console

This TypeError points to the post.js service

TypeError: undefined is not a function
    at http://localhost:9000/scripts/services/post.js:16:11

which is line 16, column 11 at the beginning of the $child method call. Also, note the second .&child() following the first. That will result in another TypeError.

return posts.$add(post).then(function(ref) {
  var postId = ref.name();
  user.$child('posts').$child(postId).$set(postId);
  return postId;
});

I will mention that even though I get this TypeError, I'm still able to successfully create a post and I see it in the Forge and in the app's post list.

Deleting a Post > TypeError: undefined is not a function

When I try to delete a post that I just created I get another TypeError related to the now deprecated .$on() method.

TypeError: undefined is not a function
    at Object.Post [as delete] (http://localhost:9000/scripts/services/post.js:27:10)
    at Scope.$scope.deletePost (http://localhost:9000/scripts/controllers/posts.js:10:14)

which points to the .$on() in this code in the post.js service

delete: function(postId) {
  if(User.signedIn()) {
    var post = Post.find(postId);
    post.$on('loaded', function(){
      var user = User.findByUsername(post.owner);
      posts.$remove(postId).then(function(){
        user.$child('posts').$remove(postId);
      });
    });
  }
}

I realize that I will have to do something like

post.$loaded(function(result) {Chapter 7. Creating your own user data using firebase
  // something related to finding user of post
  // remove post from posts collection 
  // but of course we can't use .$child()
});

but the problem is that when a JS beginner like me comes along faced with the AngularFire change logs and API, I just get lost and overwhelmed to the point of shut down. Any guidance is greatly appreciated as always.


FILES


post.js service

'use strict';

app.factory('Post', function($firebase, FIREBASE_URL, User){
  var ref = new Firebase(FIREBASE_URL + 'posts');
  var posts = $firebase(ref).$asArray();

  var Post = {
    all: posts,
    create: function(post) {
      if(User.signedIn()) {
        var user = User.getCurrent();
        post.owner = user.username;

        return posts.$add(post).then(function(ref) {
          var postId = ref.name();
          user.$child('posts').$child(postId).$set(postId);
          return postId;
        });
      }
    },
    find: function(postId) {
            return $firebase(ref.child(postId)).$asObject();
    },
    delete: function(postId) {
      if(User.signedIn()) {
        var post = Post.find(postId);
        post.$on('loaded', function(){
          var user = User.findByUsername(post.owner);
          posts.$remove(postId).then(function(){
            user.$child('posts').$remove(postId);
          });
        });
      }
    }
  };

  return Post;

});


post.js controller

'use strict';

app.controller('PostsCtrl', function($scope, $location, Post) {

  $scope.posts = Post.all;

  $scope.post = {url: 'http://', title: ''};

  $scope.deletePost = function(post) {
    Post.delete(post);
  };

});

解决方案

I was fed up, so I took a easy approach (just to get trough the tutorial). I created userRef (this should come from the User object imo):

modulename.factory("Post", function ($firebase, FIREBASE_URL, User) {
    //rest code

    var userRef = new Firebase(FIREBASE_URL + "users");

    var Post = {
        create: function(){
            //rest of the code

            return posts.$add(post).then(function (ref) {
                var postId = ref.name();

                userRef.child(user.$id).child("posts").child(postId).set(postId);

                return postId;
            });
        }
    }
});

This is just one approach, when I was searching for a good solution, I saw some more example codes. I hope it helps a little bit.

这篇关于类型错误:未定义不是一个函数在提交&安培;删除使用AngularFire和AngularJS(ThinksterIO教程第7章)后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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