后续:Meteor.users带有字符串参数
Followup: Meteor.users with string argument
我有一个遍历user_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 collaborat=Meteor.users.findOne({_id:self})时抛出一个错误。具体来说,它抛出一个类型错误,即Object 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)
相关文章:
- 从查询字符串参数推断出正确的数据类型
- 如何使用JQuery在HTML中创建包含字符串参数的引号的onclickjavascript链接
- 如何在 OnClick 函数中传递字符串参数
- 使用Angular Resource查询具有字符串参数的REST API
- 如何在JavaScript函数中将字符串参数传递给Java方法
- 为javascript函数提供可能包含特殊字符的字符串参数
- 使用查询字符串参数筛选数据
- 将查询字符串参数与谷歌地图api v3服务结合使用
- 从 URL - AngularJS 获取动态查询字符串参数
- 函数中的 Javascript 字符串参数
- 将服务字符串参数转换为数组的 Angular 内部
- 将字符串参数传递给 JavaScript 函数
- 无法通过 ajax 将字符串参数传递给 php 函数
- 使用基于查询字符串参数的 Angular 填充选择元素
- 从标记中的 href 获取查询字符串参数
- 从带有字符串参数的函数中的元素中获取值
- 将字符串参数传递给 javascript 函数 mvc
- 将查询字符串参数添加到 GridView 项模板
- 将字符串参数从 JS 视图传递到控制器
- 使用 RegEx 进行 JavaScript 字符串参数编码