gpt4 book ai didi

javascript - Meteor React - 发布/订阅不起作用

转载 作者:行者123 更新时间:2023-11-28 04:00:45 25 4
gpt4 key购买 nike

我不知道哪里出了问题,但这是我在 import/api 中的代码

import { Mongo } from 'meteor/mongo';

Blogposts = new Mongo.Collection('blogposts');

if (Meteor.isServer) {

Meteor.publish('allBlogposts', function () {
return Blogposts.find({});
});
}

在我的前端 jsx 文件中:

import React from 'react';
import ReactDOM from 'react-dom';
import TrackerReact from 'meteor/ultimatejs:tracker-react';

Blogposts = new Mongo.Collection('blogposts');

export default class BlogPage extends TrackerReact(React.Component) {

constructor(props) {
super(props);
this.state = {
subscription: {
blogposts: Meteor.subscribe('allBlogposts')
}
}
}

不过,如果我控制台日志,我似乎无法从数据库访问任何数据,我从服务器向数据库添加了两条记录。

如何才能使这个工作适用于返回这两个文档的简单控制台日志?

最佳答案

# api    
import { Mongo } from 'meteor/mongo';

export default Blogposts = new Mongo.Collection('blogposts');

if (Meteor.isServer) {

Meteor.publish('allBlogposts', function () {
return Blogposts.find({});
});
}

组件

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { withTracker } from 'meteor/react-meteor-data';

#import your api
import { Blogposts } from "whatever/the/dir/is.js

class BlogPage extends Component {

# Everything here

}
#you write this function outside your component
BlogPage.propTypes = {
currentUserPost: PropTypes.array.isRequired,
};
export default withTracker(()=>{
Meteor.subscribe("allBlogposts");
return {
currentUserPost: Blogposts.find()
}
})(BlogPage);

关于javascript - Meteor React - 发布/订阅不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47136229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com