Search code examples
javascriptmeteormeteor-autoformsimple-schema

How to access own user id in AutoForm Select2 Meteor?


I am using Select2 AutoForm and I have the following schema:

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

Now this works fantastic, but the problem is that the currently logged in username is also listed as an option. I want to remove this particulary option so I tried the following:

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

However, this.userId is not available. How can I access this variable?

EDIT: I tried to set the options client-side, but this didn't work out:

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>

I got the following exceptions:

[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

Solution

  • The problem was that I was not returning an object inside my cursor.map(). So I returned {} but this was not my desired outcome, because there was now an empty option displaying.

    However, this is how I fixed this problem:

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