使用简单模式和反应时出现未定义的集合错误(流星应用程序)
undefined collection error when using simple schema and react (meteor app)
我正在使用 meteor 1.4 并作出反应以创建一个 poll/vote 网站并在集合中使用了简单的架构。但是当我在 React 组件中订阅我的集合时,我在 chrome 控制台中收到此错误:Uncaught TypeError: Cannot read 属性 'find' of undefined.
这是合集文件:
import { Mongo } from 'meteor/mongo';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
const Polls = new Mongo.Collection('polls');
Polls.schema = new SimpleSchema({
createdAt: {
type: Date,
autoValue: function() {
return new Date()
}
},
question: {
type: String
},
answers: {
type: [String]
},
ownerId: {
type: String,
autoValue: function() {
return this.userId
}
},
votes: {
type: [Number]
}
});
Meteor.methods({
'poll.new': function() {
return Polls.insert({});
},
'poll.remove': function(poll) {
return Polls.remove(poll);
},
'poll.update': function(poll, question, answers) {
// db.users.update({"username": "tom"}, {"$set": {"documents": []}})
return Polls.update(poll._id, { $set: { question }}, { $push: {answers: { $each: answers }} });
},
'poll.vote': function(poll, vote) {
return Polls.update(poll._id, {$push: { vote } });
}
});
export default Polls;
这是反应组件:
import React, { Component } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
import { Link } from 'react-router';
import { Polls } from '../../../imports/collections/polls';
class PollsList extends Component {
render() {
return (
<div>
{this.props.polls.map(poll => {
const { question } = poll;
<Link to= {`/polls/vote/${poll._id}`} key={ poll._id }> { question } </Link>
})}
</div>
);
}
}
export default createContainer(() => {
Meteor.subscribe('polls');
return { polls: Polls.find({}).fetch() };
}, PollsList);
这是服务器内部的发布代码:
import { Meteor } from 'meteor/meteor';
import { Polls } from '../imports/collections/polls';
Meteor.startup(() => {
Meteor.publish('polls', function() {
return Polls.find({});
});
Meteor.publish('myPolls', function() {
return Polls.find({ownerId: this.userId});
});
});
因为我的 cmd 中没有任何错误,所以我无法弄清楚这里的问题是什么。
如果此错误未出现在服务器上,请专注于在此行中首次亮相您在 React 组件上的代码
return { polls: Polls.find({}).fetch() };
好像民意调查未定义,也检查民意调查导入的路径
import { Polls } from '../../../imports/collections/polls';
希望对您有所帮助。
我正在使用 meteor 1.4 并作出反应以创建一个 poll/vote 网站并在集合中使用了简单的架构。但是当我在 React 组件中订阅我的集合时,我在 chrome 控制台中收到此错误:Uncaught TypeError: Cannot read 属性 'find' of undefined.
这是合集文件:
import { Mongo } from 'meteor/mongo';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
const Polls = new Mongo.Collection('polls');
Polls.schema = new SimpleSchema({
createdAt: {
type: Date,
autoValue: function() {
return new Date()
}
},
question: {
type: String
},
answers: {
type: [String]
},
ownerId: {
type: String,
autoValue: function() {
return this.userId
}
},
votes: {
type: [Number]
}
});
Meteor.methods({
'poll.new': function() {
return Polls.insert({});
},
'poll.remove': function(poll) {
return Polls.remove(poll);
},
'poll.update': function(poll, question, answers) {
// db.users.update({"username": "tom"}, {"$set": {"documents": []}})
return Polls.update(poll._id, { $set: { question }}, { $push: {answers: { $each: answers }} });
},
'poll.vote': function(poll, vote) {
return Polls.update(poll._id, {$push: { vote } });
}
});
export default Polls;
这是反应组件:
import React, { Component } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
import { Link } from 'react-router';
import { Polls } from '../../../imports/collections/polls';
class PollsList extends Component {
render() {
return (
<div>
{this.props.polls.map(poll => {
const { question } = poll;
<Link to= {`/polls/vote/${poll._id}`} key={ poll._id }> { question } </Link>
})}
</div>
);
}
}
export default createContainer(() => {
Meteor.subscribe('polls');
return { polls: Polls.find({}).fetch() };
}, PollsList);
这是服务器内部的发布代码:
import { Meteor } from 'meteor/meteor';
import { Polls } from '../imports/collections/polls';
Meteor.startup(() => {
Meteor.publish('polls', function() {
return Polls.find({});
});
Meteor.publish('myPolls', function() {
return Polls.find({ownerId: this.userId});
});
});
因为我的 cmd 中没有任何错误,所以我无法弄清楚这里的问题是什么。
如果此错误未出现在服务器上,请专注于在此行中首次亮相您在 React 组件上的代码
return { polls: Polls.find({}).fetch() };
好像民意调查未定义,也检查民意调查导入的路径
import { Polls } from '../../../imports/collections/polls';
希望对您有所帮助。