1

我是meteorjs和MongoDB的新手。我在 Win 7 上运行流星,我在关注 isaac strack 的“Meteor.js JavaScript 框架入门”,我在他的代码之后停留在第 6 章“授予管理员权限”,但似乎当管理员登录并添加时列表中的一个项目该项目显示为第二个拆分可能是因为本地缓存和服务器同步但我在这里做错了什么?我的代码:客户端:

Meteor.subscribe('Categories');

Meteor.autosubscribe(function() {
    Meteor.subscribe("listdetails",
        Session.get('current_list'));
});

Template.categories.lists = function () {
    return lists.find({},{sort: {Category: 1}});
};

Session.set('adding_category', false);

Template.categories.new_cat = function () {
    return Session.equals('adding_category',true);
};
Template.categories.events({

    'click #btnNewCat': function (e, t) {

        Session.set('adding_category', true);

        Meteor.flush();
        focusText(t.find("#add-category"));
    },

    'keyup #add-category': function (e,t){
        if (e.which === 13)
        {
            var catVal = String(e.target.value || "");
            if (catVal)
            {
                lists.insert({Category:catVal,owner:this.userId});
                Session.set('adding_category', false);
            }
        }
    },
    'focusout #add-category': function(e,t){

        Session.set('adding_category',false);

    },
    'click .category': selectCategory
});

/////Generic Helper Functions/////

//this function puts our cursor where it needs to be.
function focusText(i,val) {
    i.focus();
    i.value = val ? val : "";
    i.select();

};//< -----This is the end tag for focusText() -----

function selectCategory(e,t){
    Session.set('current_list',this._id);
}

function addItem(list_id,item_name){
    if (!item_name&&!list_id)
        return;
    lists.update({_id:list_id},
        {$addToSet:{items:{Name:item_name}}});
}

function removeItem(list_id,item_name){
    if (!item_name&&!list_id)
        return;
    lists.update({_id:list_id},
        {$pull:{items:{Name:item_name}}});
}

function updateLendee(list_id,item_name,lendee_name){
    var l = lists.findOne({"_id":list_id ,
        "items.Name":item_name});
    if (l&&l.items)
    {
        for (var i = 0; i<l.items.length; i++)
        {
            if (l.items[i].Name === item_name)
            {
                l.items[i].LentTo = lendee_name;
            }
        }
        lists.update({"_id":list_id},{$set:{"items":l.items}});
    }
};

Template.list.items = function () {
    if (Session.equals('current_list',null)) return null;
    else
    {
        var cats = lists.findOne({_id:Session.get('current_list')});
        if (cats&&cats.items)
        {
            for(var i = 0; i<cats.items.length;i++) {
                var d = cats.items[i]; d.Lendee = d.LentTo ? d.LentTo :
                    "free"; d.LendClass = d.LentTo ?
                    "label-important" : "label-success";
            }
            return cats.items;
        }
    }
};// < ---- ending bracket for Template.list.items function ----

Template.list.list_selected = function() {
    return ((Session.get('current_list')!=null) &&
        (!Session.equals('current_list',null)));
};
Template.categories.list_status = function(){
    if (Session.equals('current_list',this._id))
        return "";
    else
        return " btn-inverse";
};
Template.list.list_adding = function(){
    return (Session.equals('list_adding',true));
};
Template.list.lendee_editing = function(){
    return (Session.equals('lendee_input',this.Name));
};

Template.list.events({
    'click #btnAddItem': function (e,t){
        Session.set('list_adding',true);
        Meteor.flush();
        focusText(t.find("#item_to_add"));
    },
    'keyup #item_to_add': function (e,t){
        if (e.which === 13)
        {
            addItem(Session.get('current_list'),e.target.value);
            Session.set('list_adding',false);
        }
    },
    'focusout #item_to_add': function(e,t){
        Session.set('list_adding',false);
    },
    'click .delete_item': function(e,t){
        removeItem(Session.get('current_list'),e.target.id);
    },
    'click .lendee' : function(e,t){
        Session.set('lendee_input',this.Name);
        Meteor.flush();
        focusText(t.find("#edit_lendee"),this.LentTo);
    },
    'keyup #edit_lendee': function (e,t){
        if (e.which === 13)
        {
            updateLendee(Session.get('current_list'),this.Name,
                e.target.value);
            Session.set('lendee_input',null);
        }
        if (e.which === 27)
        {
            Session.set('lendee_input',null);
        }
    }
});

Accounts.ui.config({
    passwordSignupFields: 'USERNAME_AND_OPTIONAL_EMAIL'
});

服务器端:

Meteor.startup(function () {
    Meteor.publish("Categories", function() {
        return lists.find({owner:this.userId},{fields:{Category:1}});
    });
    Meteor.publish("listdetails", function(category_id){
        return lists.find({_id:category_id});
    });
});

两个都:

function adminUser(userId) {
     var adminUser = Meteor.users.findOne({username:"admin"});
     return (userId && adminUser && userId === adminUser._id);
 }
 lists.allow({
     insert: function(userId, doc){
         return (adminUser(userId) || (userId && doc.owner === userId));
     },
     update: function(userId, docs, fields, modifier){
         return adminUser(userId) || _.all(docs, function(doc) {
             return doc.owner === userId;
         });
     },
     remove: function (userId, docs){
         return adminUser(userId) || _.all(docs, function(doc) {
             return doc.owner === userId;
         });
     }
 });

html:

<head>
  <title>LendLib</title>
</head>

<body>
<div style="float:right; margin-right:20px;">
    {{loginButtons align="right"}}
</div>
<div id="lendlib">
    <div id="categories-container">
        {{> categories}}
    </div>
    <div id="list">
        {{> list}}
    </div>
</div>
</body>

<template name="categories">
    <h2 class="title">
        my stuff
    </h2>
    <div id="categories" class="btn-group">
        {{#if new_cat}}
        <div class="category">
            <input type="text" id="add-category" value="" />
        </div>
        {{else}}
        <div class="category btn btn-inverse"
             id="btnNewCat">&plus;</div>
        {{/if}}
        {{#each lists}}
        <div class="category btn {{list_status}}" id="{{_id}}">
            {{Category}}
        </div>
        {{/each}}
    </div>
</template>

<template name="list">
    <ul id="lending_list">
        {{#each items}}
        <li class="lending_item alert">
            <button type="button" class="close delete_item"
                    id="{{Name}}">×</button>
            {{Name}}
            {{#if lendee_editing}}
            <input type="text" id="edit_lendee" class="span2
pull-right" value=""/>
            {{else}}
            <div class="lendee pull-right label {{LendClass}}">
                {{Lendee}}</div>
            {{/if}}
        </li>
        {{/each}}
        {{#if list_selected}}
        <li class="alert-success" id="btnAddItem">&plus;
            {{#if list_adding}}
            <input class="span4" id="item_to_add" size="32"
                   type="text">
            {{/if}}
        </li>
        {{/if}}
    </ul>
</template>
4

1 回答 1

1

经过大量调试尝试并失败后,我解决了问题,问题出在服务器端,这是原始代码:

Meteor.startup(function () {
    Meteor.publish("Categories", function() {
        return lists.find({owner:this.userId},{fields:{Category:1}});
    });
    Meteor.publish("listdetails", function(category_id){
        return lists.find({_id:category_id});
    });
});

这是固定代码:

Meteor.startup(function () {
    Meteor.publish("Categories", function() {
        return lists.find({owner:Meteor.userId},{fields:{Category:1}});
    });
    Meteor.publish("listdetails", function(category_id){
        return lists.find({_id:category_id});
    });
});

注意:更改在第 3 行完成,而不是 owner:this.userId - 我将它替换为 Meteor.userId

于 2013-08-31T09:13:46.033 回答