【问题标题】:How to show another user's profile image item with Meteor Cloudinary如何使用 Meteor Cloudinary 显示其他用户的个人资料图片项目
【发布时间】:2017-05-01 15:56:59
【问题描述】:

有 2 个问题。

首先:房间一旦启动,里面有 2 个人,所有者和接收者。我无法在房间列表中显示其他人的个人资料图像。我可以看到我自己的图像是通过 Cloudinary 上传的,保存在 profile --> profilepicId 下作为 ID,然后生成图像。

其次:进入用户页面时无法检索用户的帖子。服务器抛出 id 匹配错误。有时它也会挂起,当我转到我的个人资料页面时,我将无法查看自己的帖子。

更新:Rooms 集合的结构如下:每个房间将包含所有者、接收者、人员:[所有者、接收者]和 roomId。

服务器错误

> Exception from sub user id JS9gKeT4sXNDCwip6 Error: Match error:
> Expected string, got undefined I20170410-23:28:59.972(8)?     at
> exports.check (packages/check/match.js:34:1)
> I20170410-23:28:59.972(8)?     at Subscription.<anonymous>
> (server/server.js:88:2) I20170410-23:28:59.973(8)?     at
> (packages/reywood_publish-composite/packages/reywood_publish-composite.js:440:1)
> I20170410-23:28:59.973(8)? at Subscription._handler
> (packages/reywood_publish-composite/packages/reywood_publish-composite.js:408:1)
> at Object.exports.Match._failIfArgumentsAreNotAllChecked

publish.js

Meteor.publish('userDetails', function() {
   var fields = { username: 1, emails   : 1, profile : 1, md5hash : 1 };
   return Meteor.users.find( {}, { fields: fields });
});
Meteor.publishComposite('user', function (_id) {
  check(_id, String);
  //if (!_id) return this.ready(); doesnt help remove error
  return {
     find: function() {
        return Meteor.users.find({ _id: _id }, { 
          fields: { username: 1, emails: 1, profile : 1, md5hash: 1}
        });
     }, children: [....]
    };
 });

roomCollection.js - 我正在使用 dburles:collection-helpers

Rooms.helpers({ 
   chatPerson: function(){
      if( this.owner === Meteor.userId() ) {
         return Meteor.users.findOne({ _id: this.receiver }).username; 
      } else {
         return Meteor.users.findOne({ _id: this.owner }).username;
      }
   }
});


我可以看到用户的个人资料图片,但看不到特定用户 + 服务器匹配错误的帖子。

user.html

{{#with user}}
    {{#if profile.profilepicId}}  
      <img src="....."> <!-- can see profile pic --> 
    {{/if}}
    {{#each posts}}     <!-- cannot see posts + server match error --> 
       {{> postItem}}
    {{/each}}
{{/with}}

user.js

Template.usersShow.onCreated(function(){
   var self = this;
   self.autorun(function(){
      self.subscribe('rooms');
      self.subscribe('user', Router.current().params._id);
   });
});
Template.usersShow.helpers({
   posts: function () {
      return Posts.find({ userId: Router.current().params._id });
   }
});

【问题讨论】:

    标签: javascript node.js meteor


    【解决方案1】:

    您的出版物需要一个参数:

    Meteor.publishComposite('user', function (_id) { ... })
    

    但是您的订阅没有传递参数:

    self.subscribe('user');
    

    现在,应该永远从客户端传递当前用户的_id,因为它不受信任 - 它可能是一个欺骗值,并且无论如何它都可以在服务器上使用。

    您也不需要在这里使用publishComposite,因为您只有一个父母。相反,您可以返回一个游标数组:

    Meteor.publish('me', () => {   
      if (!this.userId) this.ready(); // since the rest is pointless
      else return [
         Meteor.users.find(this.userId, { 
           fields: { username: 1, emails: 1, profile : 1, md5hash: 1}
         }), 
         Posts.find({ userId: this.userId })
      ];
     });
    

    如果您需要在客户端查看不同用户的详细信息,则需要使用_id 参数发布该信息将其包含在您的订阅中。在这种特殊情况下,您仍然不需要发布复合。

    服务器:

    Meteor.publish('otherUser', (_id) => {   
      if (!this.userId) this.ready();
      else {
        check(_id,String);
        return [
          Meteor.users.find(_id, { 
            fields: { username: 1, emails: 1, profile : 1, md5hash: 1}
          }), 
          Posts.find({ userId: _id })
        ];
       }
     });
    

    客户:

    self.subscribe('otherUser',otherUserId); // you must pass a param
    

    你也可以简化:

    chatPersonId: function(){
      if( this.owner === Meteor.userId() ) {
         return Meteor.users.findOne({ _id: this.receiver })._id;
      } else {
         return Meteor.users.findOne({ _id: this.owner })._id;
      }
    },
    

    下至:

    chatPersonId() {
      return this.owner === Meteor.userId() ? this.receiver : this.owner;
    }
    

    【讨论】:

    • 谢谢@Michel。我曾经像上面那样做,但不知何故出现了反应性显示帖子的问题。我会尝试返回结果。这是否也意味着我可以删除第一个 userDetail 发布?
    • 您在被动显示帖子时遇到了什么问题?应该管用。是的,您的userDetails 发布是多余的。
    • 我删除了两个 pub 并替换为上面的。我可以让屏幕出现并使用Meteor.publish('user', function(){ ....。我可以查看我的帖子,但无法查看其他用户的帖子。添加check(userId, String) 不会做任何事情。服务器错误:来自子用户 ID mjCzowgwB3ZynQf6e 的异常错误:在发布者“用户”期间未检查()所有参数。整个聊天列表现在也不见了,服务器错误:`队列任务中的异常:TypeError:无法读取未定义的属性'_id',其中id是chatPerson集合助手中的那个。
    • 如果您同时查看多个其他用户,那么您可能需要继续发布您的 userDetails 出版物,因为它会找到所有用户。
    • 太好了,添加发布作品,名字出现了。如何让我的聊天对手的头像出现在房间列表中?我尝试添加说 Meteor.users.findOne({ people: { $ne: Meteor.userId() } }); 的助手,但到目前为止它出现故障并返回我自己的个人资料图片。
    猜你喜欢
    • 2015-11-18
    • 2021-03-09
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多