Post.delete(post) function not working in revised ThinksterIO Learn to Build Real-Time Web Apps (w/ AngularJS & Firebase) tutorial

*爱你&永不变心* 提交于 2019-12-12 04:57:56

问题


I've been following along with the recently revised Thinkster.io AngularJS Tutorial: Learn to Build Real-Time Web Apps but I get stuck around the 80% mark specifically with the code to delete a post. For some reason I can not delete any posts I've created with the following delete funciton the tutorial provides.

delete: function (post) {
  if (User.signedIn()){
    var user = User.getCurrent();
    if (user.username === post.owner) {
      posts.$remove(post).then(function () {
        User.posts(user.username).$remove(post.$id);
      });
    }
  }
}

When I ran after in the if block

console.log(post);

I got the string

-JWZaIrYaPYNls95jQAw 

so since post is a string the following 3 lines could never work

if (user.username === post.owner) {
      posts.$remove(post).then(function () {
        User.posts(user.username).$remove(post.$id);

They could only work if post was an object. Any ideas on how to get this delete function working?

FILES

user.js factory

'use strict';

app.factory('User', function ($firebase, FIREBASE_URL, $rootScope, $log) {
  var ref = new Firebase(FIREBASE_URL + 'users');
  var users = $firebase(ref);

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

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

        query.$loaded(function () {
            setCurrentUser(query[0].username);
        });

    });

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

  var User = {
    create: function (authUser, username) {
            var user = $firebase(ref.child(username)).$asObject();

            return user.$loaded(function() {
                user.username = username;
                /*jshint camelcase: false */
        user.md5_hash = authUser.md5_hash;
        user.$priority = authUser.uid;
                user.$save();
            });

            $log.debug(users);
    },
        findByUsername: function (username) {
            if (username) {
                return $firebase(ref.child(username)).$asObject();
            }
        },
        getCurrent: function () {
            return $rootScope.currentUser;
        },
        signedIn: function () {
            return $rootScope.currentUser !== undefined;
        },
        posts: function(username) {
            return $firebase(new Firebase(FIREBASE_URL + 'user_posts/' + username));
        }
  };

  return User;
});

post.js factory

'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.posts(user.username).$set(postId, postId);

                    return postId;
                });
            }
        },
        find: function(postId) {
            return $firebase(ref.child(postId)).$asObject();
        },
        delete: function (post) {
            if (User.signedIn()){
                var user = User.getCurrent();
                if (user.username === post.owner) {
                    posts.$remove(post).then(function () {
                        User.posts(user.username).$remove(post.$id);
                    });
                }
            }
        }
    };

    return Post;

});

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

});

posts.html view

<div class="container posts-page">

  <div class="post row" ng-repeat="(postId, post) in posts">
    <div class="col-xs-1">

    </div>
    <div class="col-md-9 col-xs-11">

      <div class="info">
        <a href="{{ post.url }}">
          {{ post.title }}
                    <span class="url">({{post.url | hostnameFromUrl}})</span>
        </a>
      </div>
      <div>
                <span>{{ post.score || 0 }} votes</span>
        &mdash;
                <span> submitted by 
                   <a href="#/user/{{post.owner}}" >{{ post.owner }}</a>
                </span>
        &mdash;
        <a href="#/posts/{{ post.$id }}">comments</a>
        <a ng-click="deletePost(post.$id)" 
               ng-show="signedIn() && post.owner === currentUser.username">delete</a>
      </div>
    </div>
    <div class="col-md-2">

    </div>

  </div>

</div>

回答1:


Look at this:

<a ng-click="deletePost(post.$id)" ...>delete</a>

You are puttiing an post.$id in it, you need to use the object post. Like this:

<a ng-click="deletePost(post)" ..>delete</a>

I haven't tried out the updated tutorial yet, thanks for the heads up



来源:https://stackoverflow.com/questions/25788083/post-deletepost-function-not-working-in-revised-thinksterio-learn-to-build-rea

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