我正在使用meteor 1.4并做出反应来创建投票/投票网站并在集合中使用简单架构。但是当我在react组件中订阅我的集合时,我在chrome console中遇到了这个错误:Uncaught TypeError:无法读取属性' find'未定义的。
这是集合文件:
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中没有任何错误,我无法弄清楚这里的问题是什么。
答案 0 :(得分:1)
如果服务器上没有出现此错误,请关注此行代码中的代码
return { polls: Polls.find({}).fetch() };
似乎未定义民意调查,请检查民意调查导入的路径
import { Polls } from '../../../imports/collections/polls';
我希望有所帮助。