Cannot map data from Mongo to meteor using reaction

Can someone help me with this question, please, I run React on Meteor, and I have this component to add some data to Mongo using TrackerReact, but I can not display it in another, filling this data in another component, I abale to console.log after loading the page, so I think that maybe loading the page before loading Mongo, any suggestion, please help, thanks:

here is Adminadd.jsx where I save 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..}

this is where i want to download 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 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 the console log, first return an empty object, then when I Gifts.find (). fetch () it will work after this:

[]
Gifts.find().fetch()
Object, Object, Object, Object, Object, Object, Object, Object]
+4
2

koolaang, Gifts. , . , Main TrackerReact. :

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

. , , .

+3

, . /, handle.ready, , .

+1

Source: https://habr.com/ru/post/1653506/


All Articles