2017-08-31 64 views
0

我知道有很多問題在那裏討論這個話題,但是,他們所有我已經嘗試返回一個空數組。我曾嘗試過:流星 - 檢索最新文檔

  • 創建新的出版物。
  • 使用for循環來檢索最後一項(不快)
  • 通過在客戶端的數據庫查看。

這裏是我的刊物:

Meteor.publish('notes-newest', function() { 
    return Notes.find().sort({$natural: -1}).limit(10); 
}); 

這裏就是我試圖訪問它:

import { Meteor } from "meteor/meteor"; 
import React from "react"; 
import { withRouter } from "react-router-dom"; 
import { Tracker } from "meteor/tracker"; 
import { Accounts } from "meteor/accounts-base"; 

import { Notes } from "../methods/methods"; 
import SubjectRoutes from "./subjectRoutes/subjectRoutes"; 
import RenderNotesBySubject from "./renderNotesBySubject"; 
import Menu from "./Menu.js"; 

class Home extends React.Component{ 
    constructor(props){ 
    super(props); 
    this.state = { 
     notes: [] 
    }; 
    } 
    componentDidMount() { 
    Meteor.subscribe('notes-newest') 
    this.tracker = Tracker.autorun(() => { 
     const notes = Notes.find().fetch() 
     if(notes == null || notes == undefined){ 
     return; 
     } 
     this.setState({ notes }) 
    }) 
    } 
    renderNewNotes(){ 
    let notes = this.state.notes; 
    let count = 10; 
    console.log(notes); 
    } 
    render(){ 
    return (
     <div> 
     <Menu /> 
     <h1></h1> 
     {this.renderNewNotes()} 
     </div> 
    ) 
    } 
} 

export default withRouter(Home); 

新代碼

import { Meteor } from "meteor/meteor"; 
import React from "react"; 
import { withRouter } from "react-router-dom"; 
import { Tracker } from "meteor/tracker"; 
import { Accounts } from "meteor/accounts-base"; 
import { createContainer } from "meteor/react-meteor-data" 

import { Notes } from "../methods/methods"; 
import SubjectRoutes from "./subjectRoutes/subjectRoutes"; 
import RenderNotesBySubject from "./renderNotesBySubject"; 
import Menu from "./Menu.js"; 

class Home extends React.Component{ 
    constructor(props){ 
    super(props); 
    this.state = { 
     notes: [] 
    }; 
    } 
    componentDidMount() { 
    // Meteor.subscribe('notes-newest') 
    this.tracker = Tracker.autorun(() => { 
     const notes = this.props.list 
     if(notes == null || notes == undefined){ 
     return; 
     } 
     this.setState({ notes }) 
    }) 
    } 
    renderNewNotes(){ 
    let notes = this.state.notes; 
    let count = 10; 
    console.log(notes); 
    } 
    render(){ 
    return (
     <div> 
     <Menu /> 
     <h1></h1> 
     {this.renderNewNotes()} 
     </div> 
    ) 
    } 
} 

export default createContainer(({props})=>{ 
    Meteor.subscribe("notes-newest", props); 
    return{ 
    list: Notes.find().fetch(), 
    } 
}, Home); 

回答

0

publication似乎好好地。所有你需要的是使用數據容器。我建議使用react-meteor-data來在氣象中使用反應性數據加載。

創建一個更高階的類來包裝你的數據,並將你現有的組件作爲呈現組件來呈現數據容器提供的數據。

希望下面的代碼片斷幫助:

export default createContainer(({props})=>{ 
    Meteor.subscribe("data.fetch", props); 
    return{ 
    list: CollectionName.find().fetch(), 
    } 
}, PresentationComponent); 

說明:createComponent將確保數據的反應,它會發送檢索到的數據將作爲PresentationComponentprops

+0

仍然沒有運氣.......我上傳的新代碼,我不是一個真正的有經驗的反應或流星用戶等都可以你檢查出來,並告訴我,如果我這樣做是正確的? –

+0

好吧,這是我現在的位置,在控制檯有一個錯誤,說:「從子筆記異常 - 最新ID 9GfWBt7xs5HRPNeoY TypeError:Notes.find(...)。sort不是一個函數」。這就是爲什麼當我console.log(this.props.list)它返回一個空數組。我的出版物可能會出現問題,而不是數據容器? –

0

您將mongodb本機語法與Meteor mongo語法混合使用。您需要:

Meteor.publish('notes-newest', function() { 
    return Notes.find({},{sort: {$natural: -1}, limit: 10}); 
}); 

,並在客戶端上同上:

list: Notes.find({},{sort: {$natural: -1}, limit: 10}); 
+0

當我這樣做時,它就像我想要的那樣工作,但是當用戶插入文檔時,它會被添加到列表的底部。我將如何得到它,以便它進入列表的頂部 –

+0

'$ natural'被設計爲可以直接在'.find()。hint({$ natural:-1})在mongodb中使用。 AFAIK你不能從Meteor訪問'.hint()'。我建議的是在創建記錄時定義'createdAt:'鍵並將其設置爲'new Date()',然後對其進行排序。 –

+0

謝謝!我遇到了一個小錯誤,但我決定把它放在一個單獨的問題在這裏:https://stackoverflow.com/questions/46045393/meteor-sorting-by-timestamp –