2017-08-31 62 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 –