2015-10-01 55 views
2

我使用自動窗體選擇二,我有以下模式:如何在AutoForm Select2 Meteor中訪問自己的用戶ID?

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) 
[email protected][native code] 
[email protected]://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:18 
[email protected]://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:152:11 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3037:52 
[email protected]://localhost:3000/packages/spacebars.js?1aedcc2aa3ae9ff5d860d73516110cedd77c033e:167:23 
http://localhost:3000/packages/aldeed_autoform.js?8d8955f3aabfe2ab46c89e06e9d94100df40ae6d:7000:27 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
with:[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:237:18 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:588:34 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1882:29 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2612:19 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1952:30 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1949:24 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1948:25 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1966:23 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2011:20 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1473:31 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1415:22 
[native code] 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1424:11 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2030:53 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2028:28 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1872:24 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
DynamicTemplate:[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:368:22 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:507:24 
[email protected]://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] [email protected]://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:218:27 (meteor.js, line 880) 
[email protected]://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:417:15 
[email protected]://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:366:21 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3330:26 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1779:31 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:523:15 
[email protected]://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) 
[email protected][native code] 
[email protected]://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:18 
[email protected]://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:152:11 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3037:52 
[email protected]://localhost:3000/packages/spacebars.js?1aedcc2aa3ae9ff5d860d73516110cedd77c033e:167:23 
http://localhost:3000/packages/aldeed_autoform.js?8d8955f3aabfe2ab46c89e06e9d94100df40ae6d:7000:27 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
with:[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:237:18 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:588:34 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1882:29 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2612:19 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1952:30 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1949:24 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1948:25 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1966:23 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2011:20 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1473:31 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1415:22 
[native code] 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1424:11 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2030:53 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2028:28 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1872:24 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1870:54 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
DynamicTemplate:[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1869:34 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:368:22 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:507:24 
[email protected]://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] [email protected]://localhost:3000/packages/aldeed_autoform-select2.js?24d34b2898684fa0e8487085f4278d6dbda6a9c4:218:27 (meteor.js, line 880) 
[email protected]://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:417:15 
[email protected]://localhost:3000/packages/aldeed_template-extension.js?198fda9390ae7785a7535751677cd8247508c9eb:366:21 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3330:26 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:3671:16 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:2211:16 
http://localhost:3000/packages/blaze.js?9391df93ba5076c2cfc61ee68724eb79b65f00d9:1779:31 
[email protected]://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:523:15 
[email protected]://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:365:23 
+0

有了自動窗體您可以選擇2指定模板內或架構中的選項。如果您在模板中指定它們,您將擁有'Meteor.userId()' – challett

回答

0

問題是我沒有在內部返回一個對象。 所以我回來了{}但這不是我想要的結果,因爲現在有一個空的選項顯示。

不過,我這是怎麼固定的這個問題:

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}; 
       }); 
      } 
     } 
    } 
}); 
相關問題