简体   繁体   中英

How to show another user's profile image item with Meteor Cloudinary

Theres 2 issues.

Firstly : Once a room is started, theres 2 people in it, owner and receiver. I am unable to display the other person's profile image in the list of rooms. I can see my own image thats is uploaded via Cloudinary, saved under profile --> profilepicId as an Id which then generates an image.

Secondly : I cannot retrieve posts of a user when I go to user's page. Server throws a id match error. Sometimes it also hangs and I will not be able to view my own posts when i go to my profile page.

Update : The Rooms collection are structure as follows: Each room will contain owner, receiver, people:[ owner , receiver ] and roomId.

Server error

 > 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 - im using 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;
      }
   }
});


I can see the user's profile image but not the posts of a specific user + server match error.

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 });
   }
});

Your publication expects a parameter:

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

But your subscription is not passing a param:

self.subscribe('user');

Now, one should never pass the current user's _id from the client as it is not trusted - it could be a spoofed value and it's available on the server anyway.

Also you don't need to use publishComposite here because you only ever have one parent. Instead you can return an array of cursors:

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 })
  ];
 });

If you need to see the details of a different user on the client then you need to publish that with an _id parameter and include that in your subscription. You still don't need publish-composite in this particular case.

server:

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 })
    ];
   }
 });

client:

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

Also you can simplify:

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

Down to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM