簡體   English   中英

如何使用Meteor Cloudinary顯示另一個用戶的個人資料圖像項目

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

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

您的出版物需要一個參數:

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參數將其發布, 並將其包括在訂閱中。 在這種特殊情況下,您仍然不需要publish-composite。

服務器:

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

暫無
暫無

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

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