2

我正在使用 Select2 AutoForm,并且具有以下架构:

Schema = {};
Schema.users = new SimpleSchema({
    users: {
        type: [String],
        autoform: {
            type: "select2",
            options: function () {
                return Users.find().map(function (user) {
                    return {label: user.username, value: user._id};
                });
            }
        }
    }
});

现在这很好用,但问题是当前登录的用户名也被列为一个选项。我想删除这个特别的选项,所以我尝试了以下操作:

Schema = {};
Schema.users = new SimpleSchema({
    users: {
        type: [String],
        autoform: {
            type: "select2",
            options: function () {
                var userId = this.userId;
                return Users.find().map(function (user) {
                    if(user._id != userId) return {label: user.username, value: user._id};
                });
            }
        }
    }
});

但是,this.userId不可用。我怎样才能访问这个变量?

编辑: 我尝试在客户端设置选项,但这没有成功:

Template.users.helpers({
    userFormSchema: function () {
        return Schema.users;
    },
    usersOptions: function () {
        return Users.find().map(function (user) {
            if (Meteor.userId() != user._id) return {label: user.username, value: user._id};
        });
    }
});

<template name="users">
    <div class="page-content">
    {{#autoForm schema=userFormSchema id="insertUserForm" type="method" meteormethod="userInsert"}}
        {{> afFieldInput name="users" type="select2" multiple=true options=usersOptions}}
        <button type="submit" class="btn btn-primary">Submit</button>
    {{/autoForm}}
    </div>
</template>

我有以下例外:

[Log] Exception in template helper: http://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:153:14 (meteor.js, line 880)
forEach@[native code]
forEach@http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:18
contextAdjust@http://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:152:11
afFieldInputContext@http://localhost:3000/packages/aldeed_autoform.js?8d8955f3aabfe2ab46c89e06e9d94100df40ae6d:7107:91
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2986:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1650:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3038:71
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3037:52
call@http://localhost:3000/packages/spacebars.js?1aedcc2aa3ae9ff5d860d73516110cedd77c033e:167:23
http://localhost:3000/packages/aldeed_autoform.js?8d8955f3aabfe2ab46c89e06e9d94100df40ae6d:7000:27
wrappedArgFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2900:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2613:30
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1872:24
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
with:setData@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34
_compute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36
Computation@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:237:18
autorun@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:588:34
autorun@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1882:29
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2612:19
fireCallbacks@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1952:30
nonreactive@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1949:24
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
_fireCallbacks@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1948:25
_createView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1966:23
_materializeView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2011:20
materializeDOMInner@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1473:31
_materializeDOM@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1415:22
[native code]
_materializeDOM@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1424:11
doMaterialize@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2030:53
nonreactive@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13
doRender@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2028:28
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1872:24
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
DynamicTemplate:materialize@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34
_compute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36
_recompute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:368:22
_runFlush@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:507:24
onGlobalMessage@http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:365:23
[Log] Exception from Tracker afterFlush function: (meteor.js, line 880)
[Log] TypeError: undefined is not an object (evaluating 'template.data.atts') (meteor.js, line 880)
[Log] rendered@http://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:218:27 (meteor.js, line 880)
runTemplateHooks@http://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:417:15
templateExtensionMasterHook@http://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:366:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3330:26
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
fireCallbacks@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3326:37
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3419:18
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1780:18
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1779:31
_runFlush@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:523:15
onGlobalMessage@http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:365:23
[Log] Exception in template helper: http://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:153:14 (meteor.js, line 880)
forEach@[native code]
forEach@http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:18
contextAdjust@http://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:152:11
afFieldInputContext@http://localhost:3000/packages/aldeed_autoform.js?8d8955f3aabfe2ab46c89e06e9d94100df40ae6d:7107:91
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2986:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1650:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3038:71
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3037:52
call@http://localhost:3000/packages/spacebars.js?1aedcc2aa3ae9ff5d860d73516110cedd77c033e:167:23
http://localhost:3000/packages/aldeed_autoform.js?8d8955f3aabfe2ab46c89e06e9d94100df40ae6d:7000:27
wrappedArgFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2900:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2613:30
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1872:24
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
with:setData@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34
_compute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36
Computation@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:237:18
autorun@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:588:34
autorun@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1882:29
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2612:19
fireCallbacks@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1952:30
nonreactive@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1949:24
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
_fireCallbacks@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1948:25
_createView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1966:23
_materializeView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2011:20
materializeDOMInner@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1473:31
_materializeDOM@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1415:22
[native code]
_materializeDOM@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1424:11
doMaterialize@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2030:53
nonreactive@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13
doRender@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2028:28
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1872:24
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
DynamicTemplate:materialize@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34
_compute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36
_recompute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:368:22
_runFlush@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:507:24
onGlobalMessage@http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:365:23
[Log] Exception from Tracker afterFlush function: (meteor.js, line 880)
[Log] TypeError: undefined is not an object (evaluating 'template.data.atts') (meteor.js, line 880)
[Log] rendered@http://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:218:27 (meteor.js, line 880)
runTemplateHooks@http://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:417:15
templateExtensionMasterHook@http://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:366:21
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3330:26
_withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16
fireCallbacks@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3326:37
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3419:18
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1780:18
_withCurrentView@http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1779:31
_runFlush@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:523:15
onGlobalMessage@http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:365:23
4

1 回答 1

0

问题是我没有在cursor.map(). 所以我回来{}了,但这不是我想要的结果,因为现在显示的是一个空选项。

但是,这就是我解决此问题的方法:

Schema = {};
Schema.users = new SimpleSchema({
    users: {
        type: [String],
        autoform: {
            type: "select2",
            options: function () {
                return Users.find({_id: {$ne: Meteor.userId()}}).map(function (user) {
                    return {label: user.username, value: user._id};
                });
            }
        }
    }
});
于 2015-10-02T07:12:10.473 回答