我想从db中按大多数喜欢的顺序从最不重要的文件中读取文件,并且一直运行到一个错误。我创建了一些喜欢1,2和3的文档,返回的顺序是2,3,1。这很奇怪,因为当我第一次启动服务器时,它工作正常,但是我发现在大约20在我的项目上工作了很多分钟(而不是触及我即将发布的代码),我意识到它并没有按照正确的顺序返回文档。这可能是流星中的一个错误吗?或者这是我的问题?无论如何,这里是我试图按顺序获取文档的代码。Mongo排序不返回数据的顺序
renderNotesByLike.js
import React from "react";
import { Tracker } from "meteor/tracker";
import { Link, withRouter } from "react-router-dom"
import { Notes } from "./../../methods/methods";
class RenderNotesByLike extends React.Component{
constructor(props){
super(props);
this.state = {
notes: []
};
}
renderNotes(notes){
return notes.map((note) => {
return(
<div key={note._id} className="note-list" onClick={() => {this.props.history.push(`/fullSize/${note._id}`)}}>
<div className="left inline">
<p><strong>{note.title}</strong></p>
<span className="removeOnSmallDevice">{note.userEmail}</span>
</div>
<div className="right inline">
<span>Subject: <strong>{note.subject}, {note.unit}</strong></span>
<br />
<span className="removeOnSmallDevice">⬆ {note.likes.length} ⬇ {note.dislikes.length}</span>
</div>
</div>
)
})
}
componentDidMount() {
this.tracker = Tracker.autorun(() => {
Meteor.subscribe('notes');
const notes = Notes.find({subject: this.props.subject}, {sort: {likes: -1}}).fetch();
notes.map((note) => {console.log(note.likes.length)})
this.setState({ notes })
});
}
componentWillReceiveProps(nextProps) {
this.tracker = Tracker.autorun(() => {
Meteor.subscribe('notes');
const notes = Notes.find({subject: nextProps.subject}, {sort: {likes: -1}}).fetch();
this.setState({ notes });
});
}
componentWillUnmount() {
this.tracker.stop()
}
render(){
return(
<div className="center">
{this.renderNotes(this.state.notes)}
</div>
)
}
}
export default withRouter(RenderNotesByLike);
为notes
的发布是非常基本的:
Meteor.publish('notes', function() {
return Notes.find()
});
我也知道可能出现的问题是因为我发布的所有笔记,我有发布我想要过滤的内容。但我用与CreatedAt
属性完全相同的方式来完成,而且工作得很好。
示例数据
cloudinaryData:
{data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
createdAt:
1506224240000
description:""
dislikes:[]
imageURL:["AImageURL.jpg"]
likes:["[email protected]"]
subject:"Food"
title:"a"
unit:"a"
userEmail:"[email protected]"
userId:"rSGkexdzzPnckiGbd"
_id:"GPJa8qTZyDHPkpuYo"
__proto__:Object
注架构:
"notes.insert"(noteInfo){
noteInfo.imageURL.map((url) => {
const URLSchema = new SimpleSchema({
imageURL:{
type:String,
label:"Your image URL",
regEx: SimpleSchema.RegEx.Url
}
}).validate({ imageURL:url })
})
Notes.insert({
title: noteInfo.title,
subject: noteInfo.subject,
description: noteInfo.description,
imageURL: noteInfo.imageURL,
userId: noteInfo.userId,
userEmail: noteInfo.userEmail,
unit: noteInfo.unit,
likes: [],
dislikes: [],
createdAt: noteInfo.createdAt,
cloudinaryData: noteInfo.cloudinaryData
})
console.log("Note Inserted", noteInfo)
}
您能否显示您的'Notes'模式和数据示例? – Styx
@Styx更新超过 –