1

我设法在前端显示了海报的用户 ID,并尝试了许多不同的方法来显示用户名而不是 ID。找不到什么问题或该做什么。

要发布的表格

    <form ng-show="$root.currentUser">
    <label>Name</label>
    <input ng-model="newPost.name">
    <label>Description</label>
    <input ng-model="newPost.description">
    <button ng-click="newPost.owner=$root.currentUser._id; posts.push(newPost)">Add</button>    
    </form>

控制器

    angular.module('posts').controller('PostsCtrl', ['$scope', '$meteor',
function($scope, $meteor){

    $scope.posts = $meteor.collection(Posts);
    $scope.remove = function(post){
        $scope.posts.remove(post);
    };
}]);

模型:

    Posts = new Mongo.Collection("posts");    
        Posts.allow({
            insert: function (userId, post) {
                return userId && post.owner === userId;
            },
            update: function (userId, post, fields, modifier) {
                return userId && post.owner === userId;
            },
            remove: function (userId, post) {
                return userId && post.owner === userId;
    }
    });

编辑: HTML:

    <p class="post-user">{{post.owner}}</p>

现在一切正常,但这次它显示的是用户 ID 而不是用户名。

提前致谢!

4

2 回答 2

1

如果您认为其他所有内容都是正确的,请尝试以下操作:<p class="post-user">{{post.owner}}</p>. 正如我所说,我不知道流星,也许你的代码是正确的。但是当您添加帖子时,您将owner属性设置为用户名。我找不到你设置的地方newPost.username

于 2015-07-05T11:43:44.210 回答
1

如果向下滚动此步骤,您可以找到 Meteor 的用户对象的默认结构:http: //angular-meteor.com/tutorial/step_09

你在哪里定义用户名?如果你不定义它,它就不会在任何地方。通常用户信息保存在“profile”键下,但这取决于您用于登录的服务(Facebook、Email-Password)和您自己保存用户数据的实现。

于 2015-07-07T00:24:14.360 回答