简体   繁体   English

JSX不渲染

[英]JSX Not rendering

I'm working through an online course on React and Meteor. 我正在完成有关React和Meteor的在线课程。 Whenever I hit the submit button it registers the new player in the database, but it doesn't actually show p on the client side. 每当我按下“提交”按钮时,它都会在数据库中注册新播放器,但实际上不会在客户端显示p。 I get the following in the console: 我在控制台中得到以下内容:

Exception from Tracker recompute function: Invariant Violation: Objects are not valid as a React child (found: object with keys {playerName}). Tracker重新计算功能的异常:永久违反:对象作为React子对象无效(找到:带有键{playerName}的对象)。 If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons." 如果您打算呈现子级集合,请改用数组,或使用React附加组件中的createFragment(object)包装对象。”

 import React from 'react';
 import ReactDOM from 'react-dom';
 import {Meteor} from 'meteor/meteor';
 import {Tracker} from 'meteor/tracker';

 import {Players} from './../imports/api/players.js';

 const renderPlayers = (playersList) => {

    return playersList.map( (player) =>{
        return <p key={player._id}>{player.name} has {player.score} points!</p>;
    });
 };

const handleSubmit = (e) => {
    let playerName = e.target.playerName.value;

    e.preventDefault();

    if (playerName) {

    e.target.playerName.value = '';

    Players.insert({
    name: playerName,
    score: 0
    });
   }
 };
 Meteor.startup( () => {

    Tracker.autorun( () => {
        let players = Players.find().fetch();
        let title = 'Score Keeper';
        let name = 'Bill';
        let jsx = (
            <div>
                <h1>{title}</h1>
                <p>Hello {name}!</p>
                <p>Second p</p>
                {renderPlayers(players)}
                <form onSubmit={handleSubmit}>
                    <input type="text" name="playerName" placeholder="Player Name"/>
                    <button>Add Player</button>
                </form>
            </div>
            );
        ReactDOM.render(jsx, document.getElementById('app'));
    });
});

I'm almost positive that this line: 我对此行几乎是肯定的:
let players = Players.find().fetch(); is not returning an array as you expected. 没有返回您期望的数组。
I'm guessing .fetch is returning an object (maybe with .data in it?) and that's why you are getting this error. 我猜.fetch返回一个对象(也许其中包含.data ?),这就是为什么出现此错误的原因。
Try logging the players object after .fetch finished and see what's inside this object. 尝试在.fetch完成后记录players对象,然后查看该对象内部的内容。

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

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