2017-10-19 129 views
0

我想獲取meteorJS中所有註冊用戶的數據。根據meteorJS和stackOverflow的Doc,在渲染函數中,我嘗試了一些方法(結果如提交所示)。我只收到一個空數組或訂閱新對象時,我居然有db.users 2個用戶帳戶:db.users.find的獲取meteor Js中所有註冊用戶的數據Js

結果()

{ "_id" : "uFdKrgSyeBr6msxsH", "createdAt" : ISODate("2017-10-19T12:35:26.930Z"), "services" : { "password" : { "bcrypt" : "$2a$10$.LP2ftnNFDNx1gB7ouaMZ.OwXWLk89iDL6qjOuc.CVebtR/ziiv1K" }, "resume" : { "loginTokens" : [ ] } }, "username" : "AAA" } 
{ "_id" : "sJXS2YZ3vENHK2TYT", "createdAt" : ISODate("2017-10-19T12:35:46.266Z"), "services" : { "password" : { "bcrypt" : "$2a$10$TMbeew0aQ/L71YCHjSMoveyf3Kj7Vf0uNnZc7D0OnVtze.ZcuuoE6" }, "resume" : { "loginTokens" : [ { "when" : ISODate("2017-10-19T12:35:46.274Z"), "hashedToken" : "4WpMPUrqydYyKSStCWayBVubNKhgUiLmRio7N1902mg=" } ] } }, "username" : "BBB" } 

https://github.com/LeMueller/meteor_react_chatroom

也許有人知道的問題。提前致謝。

代碼在客戶端:

import { Meteor } from 'meteor/meteor'; 

Meteor.startup(() => { 
    Meteor.publish('allUsers', function(){ 
    return Meteor.users.find({},{ 
     fields:{ 
      _id:1, 
      username:1 
     } 
    }); 
    }); 
}); 

回答

1

你是你組件的渲染方法中訂閱您的 'ALLUSERS':在服務器

import React, {Component} from 'react'; 
import ReactDOM from 'react-dom'; 
import { Meteor } from 'meteor/meteor'; 
import {DialogServer} from './dialogServer.js'; 

export default class SendArea extends Component{ 

    constructor(props){ 
     super(props); 
    } 

    handleSubmit(event) { 
     event.preventDefault(); 

     // Find the text field via the React ref 
     const text = ReactDOM.findDOMNode(this.refs.textInput).value.trim(); 

     if(Meteor.userId()) 

     DialogServer.insert({ 
      text: text, 
      owner: Meteor.userId(),   // _id of logged in user 
      username: Meteor.user().username, // username of logged in user 
      createdAt: new Date(), // current time 
     }); 

     // Clear form 
     ReactDOM.findDOMNode(this.refs.textInput).value = ''; 
    } 

    render(){ 
     let allUsersDate=Meteor.subscribe('allUsers'); //{subscriptionId: "4nHErSSjJLJesWJcq", stop: ƒ, ready: ƒ} 
     let allUsersD = Meteor.users.find({}).fetch(); //Array(0) 
     return(
      <div> 
      <form className="new_message" onSubmit={this.handleSubmit.bind(this)}> 
       <input type='text' ref="textInput" placeholder="message to send"/> 
       <button onClick={this.handleSubmit.bind(this)}>send</button> 
      </form> 
      </div> 
     ) 
    } 
} 

代碼

let allUsersDate=Meteor.subscribe('allUsers'); //{subscriptionId: "4nHErSSjJLJesWJcq", stop: ƒ, ready: ƒ} 
let allUsersD = Meteor.users.find({}).fetch(); //Array(0) 

這不是正確的方式並導致幾個問題。首先,您需要檢查您的訂閱,如果它是ready,然後才能通過Meteor.users.find使用這些數據。

let allUsersDate=Meteor.subscribe('allUsers'); 
if (allUsersDate.ready()) { 
    let allUsersD = Meteor.users.find({}).fetch(); // will return all users 
} 

但是,即使你會做如上圖所示,您render方法可以再次調用甚至在你的訂閱已經準備就緒。渲染方法僅用於渲染而不用於數據處理。

您應該瞭解如何使用createContainercreate a container for Meteor subscriptions並將此數據傳遞到您的組件。這也使得你的組件更加可重用並且更容易測試(因爲所需的數據是通過道具注入的)。

+0

我會試試看。非常感謝。 –