簡體   English   中英

后續:Meteor.users與字符串參數

[英]Followup: Meteor.users with string argument

我有一個遍歷用戶_id字符串的模板。

    <div class="page-header"><h4>Colleagues</h4></div>
        <ul class="list-group">
            {{#each colleague}}
                <li class="list-group-item">
                    <div class="colleague">
                    {{nameOrEmail}}
                    </div>
                </li>
            {{/each}}
        </ul>

當我打電話

nameOrEmail: function () {
    if (myTeam.ready()) {
        console.log ("into function nameorEmail");
        console.log(this);
        var self=this;
        if (self) { 
        var colleague=Meteor.users.findOne({_id:self});
        console.log(colleague);
        if (colleague.username)
            return colleague.username;
        else if (colleague.emails.count()>0)
            return collegue.emails[0].address;
        }}
    return null;

在模板中,執行var colleague = Meteor.users.findOne({_ id:self})時會拋出錯誤。 具體來說,它引發類型錯誤,即對象0沒有方法'substr'

manageAccount.js?a86dcce62b84091cf50c653de6d2db7f0102a155:59
Exception from Deps recompute function: TypeError: Object 0 has no method 'substr'
at http://localhost:3000/packages    /minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1211:33
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:159:22)
at isOperatorObject (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1210:5)
at compileValueSelector (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1406:14)
at http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1386:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1369:5)
at _.extend._compileSelector (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1346:12)
at new Minimongo.Matcher (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1289:27)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:142:20) 

我已經確認可以從Javascript控制台運行findOne命令並獲得預期的結果。 我還通過流星函數跟蹤了程序的執行。 在我看來,selector.js文件中的_.type函數可能是罪魁禍首。

LocalCollection._f = {
// XXX for _all and _in, consider building 'inquery' at compile time..

_type: function (v) {
if (typeof v === "number")
  return 1;
if (typeof v === "string")
  return 2;
if (typeof v === "boolean")
  return 8;
if (isArray(v))
  return 4;
if (v === null)
  return 10;
if (v instanceof RegExp)
  // note that typeof(/x/) === "object"
  return 11;
if (typeof v === "function")
  return 13;
if (v instanceof Date)
  return 9;
if (EJSON.isBinary(v))
  return 5;
if (v instanceof LocalCollection._ObjectID)
  return 7;
return 3; // object

當我使用v作為字符串進行跟蹤時(在Javascript調試器局部變量監視器中確認):

String {0: "d", 1: "e", 2: "P", 3: "k", 4: "j", 5: "N", 6: "j", 7: "4", 8: "8", 9: "T", 10: "e", 11: "F", 12: "v", 13: "d", 14: "z", 15: "v", 16: "K", length: 17} 

它返回值3(對象)而不是2(字符串)。

我懷疑這是我問題的根本原因。 這是流星中的蟲子嗎? 有誰知道如何解決此問題?

在此先感謝您的幫助。

好的,我顯式地將字符串轉換為字符串類型,現在可以正常工作了。 很奇怪。

        if (self) { 
        var colleague=Meteor.users.findOne({_id:String(self)});
        console.log(colleague);
        if (colleague.username)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM