gpt4 book ai didi

javascript - 收集助手抛出一个未定义的错误

转载 作者:行者123 更新时间:2023-11-30 16:20:27 25 4
gpt4 key购买 nike

我正在尝试设置“房间”列表。预期顺序:

  1. 在他/她的个人资料页面上点击用户名
  2. 检查现有房间。如果是,则去那个房间,如果不是,则设置新房间。

我同时使用 dburles:collection-helpersreywood:publish-composite

它向我抛出这个错误。

TypeError: Cannot read property 'username' of undefined at Document.Rooms.helpers.recName (rooms.js:18)

And line 18 is: return Meteor.users.findOne({ _id: this.receiver }).username;

即_id:this.receiver 未定义。

我还尝试在收集助手中添加保护性检查,但错误仍然存​​在。 IE。返回 user && user.username 例如

我注意到的一件事是,我注意到当我点击用户时,它会转到链接到用户 ID 的房间。但是,当我单击返回时,它会跳转到一个具有无法识别的不同 ID 的空白房间。


相关代码:

服务器发布

Meteor.publish("onlusers", function (){
return Meteor.users.find({});
});

Rooms.js 收集助手

Rooms.helpers({
recName: function() {
return Meteor.users.findOne({ _id: this.receiver }).username;
}
});

User.js(用于个人资料页面事件)

Template.usersShow.events({
'click .user': function() {
var receiver = this._id;
Session.set('chatId', this._id);
var res = Rooms.findOne({
$or: [
{ owner : this._id },
{ receiver : this._id }
]
});
if(res){
Router.go('roomDetail', { "_id" : res._id });
} else {
var newRoom = Rooms.insert({
owner : Meteor.userId(),
receiver : receiver,
username : Meteor.user().username,
});
Session.set('roomid', newRoom);
Router.go('roomDetail', { "_id" : newRoom });
}
}
});

最佳答案

您的诊断:

_id: this.receiver is undefined.

可能会产生误导。也有可能是当您的助手运行时用户订阅未完全加载。前几天我正在帮助其他人解决类似的发布复合问题 - 当 parent 准备好但 child 可能尚未完成加载时,订阅被标记为准备就绪。我认为这是 publish-composite 中的一个错误,所有相关对象确实需要在订阅可以标记为就绪之前存在。

而不是返回:

return Meteor.users.findOne({ _id: this.receiver }).username;

你可以这样做:

var user = Meteor.users.findOne({ _id: this.receiver });
return user && user.username;

因此在用户对象加载之前您不会得到任何返回,但您不会抛出错误。

关于javascript - 收集助手抛出一个未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34834839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com