我知道有很多問題在那裏討論這個話題,但是,他們所有我已經嘗試返回一個空數組。我曾嘗試過:流星 - 檢索最新文檔
- 創建新的出版物。
- 使用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);
仍然沒有運氣.......我上傳的新代碼,我不是一個真正的有經驗的反應或流星用戶等都可以你檢查出來,並告訴我,如果我這樣做是正確的? –
好吧,這是我現在的位置,在控制檯有一個錯誤,說:「從子筆記異常 - 最新ID 9GfWBt7xs5HRPNeoY TypeError:Notes.find(...)。sort不是一個函數」。這就是爲什麼當我console.log(this.props.list)它返回一個空數組。我的出版物可能會出現問題,而不是數據容器? –