I know there are many questions out there discussing this topic but, all of them I have tried returns an empty array. I have tried:
- creating a new publication.
- using for loop to retrieve last item (not fast)
- looking through the db on the client side.
Here is my publication:
Meteor.publish('notes-newest', function () {
return Notes.find().sort({$natural: -1}).limit(10);
});
Here is where I am trying to access it:
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);
New Code
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);