简体   繁体   English

使用react无法在流星中显示来自Mongo的数据

[英]Can't display data from Mongo in meteor using react

Can anyone help me with this issue please, I'm running React on Meteor and i have this Component to add some data to Mongo using TrackerReact, But i cant display them in another populate these data in another component, I'm abale to console.log data after the page load, therefore i think maybe the page load before the Mongo data loads, any suggestion please help,Thank you: 任何人都可以帮助我解决这个问题,我正在运行React on Meteor并且我有这个组件使用TrackerReact向Mongo添加一些数据,但我不能在另一个组件中显示它们在另一个组件中填充这些数据,我是掌握到控制台页面加载后的.log数据,因此我想可能是Mongo数据加载前的页面加载,有任何建议请帮忙,谢谢:

here is Adminadd.jsx where i save on mongo: 这里是Adminadd.jsx,我在mongo上保存:

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

Gifts= new Mongo.Collection("gifts");

export default class AdminAdd extends TrackerReact(React.Component){
addGift(event){
    event.preventDefault();

    giftName=this.refs.giftname.value.trim();
    giftCity=this.refs.giftcity.value.trim();
    giftActive=this.refs.giftactive.checked;

    Gifts.insert({
        name: giftName,
        city: giftCity,
      active: giftActive,
    });

    this.refs.giftname.value="";
    this.refs.giftcity.value="";
    this.refs.giftactive.checked=false;
}

render(){..RenderStuff..}

here is where i want to load them: 这是我想要加载它们的地方:

import React from 'react';
import Gift from './components/Gift.jsx'
export default class Main extends React.Component{
allgifts(){
    return Gifts.find().fetch();
}
render(){
    console.log(allgifts());
    return(
        <div className="row">
        {this.allgifts().map((gift)=>{
            return
                <Gift gift={gift} key={gift._id}/>})}
        </div>
    )}}

and the Gift Component: 和礼品组件:

import React from 'react';
export default class Gift extends React.Component{
render(){
    return(
        <div>
            <div>
                <div>{this.props.gift.name}</div>
                <div>{this.props.gift.city}</div>
                <div>{this.props.gift.active}</div>
            </div>
        </div>
    )}}

and this is console log, at first return empty object, then when i Gifts.find().fetch() it manual it works after that: 这是控制台日志,首先返回空对象,然后当我Gifts.find()。fetch()它手动它之后工作:

[]
Gifts.find().fetch()
Object, Object, Object, Object, Object, Object, Object, Object]

As koolaang said, the template may be rendered for the first time before the Gifts collection is loaded. 正如koolaang所说,模板可能会在加载Gifts集合之前第一次呈现。 The real issue is however that it's not reactively re-rendered when the collection is loaded. 然而,真正的问题是,在加载集合时,它不会被动地重新渲染。 This is because you haven't wrapped your Main component in TrackerReact . 这是因为您尚未在TrackerReact包装Main组件。 So you need to fix it like this: 所以你需要像这样解决它:

export default class Main extends TrackerReact(React.Component) { ... }

Now the component should be re-rendered once the collection loads. 现在,一旦集合加载,就应该重新渲染组件。 You can check for subscription readiness if you want to implement a loading-state, but it's not required. 如果要实现加载状态,可以检查订阅准备情况,但这不是必需的。

it is because, it takes time for the data from backend to your frontend. 这是因为从后端到前端的数据需要时间。 if you are using publish/subscribe you should use handle.ready to wait for the data to get to the frontend. 如果您使用发布/订阅,则应使用handle.ready来等待数据到达前端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM