METEOR REACT SEARCH和DISPLAY onChange用户输入需要帮助解决错误

时间:2017-02-19 00:42:58

标签: javascript reactjs meteor meteor-react

这是输入代码:

Title: <input type="text" ref="searchTitle" onChange={this.searchTitle}/>

这是onChange事件处理程序:

searchTitle(event) {
    this.setState({
      show_article_editable_list: <Article_Editable_List articleTitle={event.target.value}/>,
    });
  }

这是订阅方法,其中包含以前代码中的事件值:

export default createContainer( (props) => {
  const articleTitle = props.articleTitle;
  Meteor.subscribe('search_results', articleTitle);

  return { articles: Articles.find({}).fetch() };
}, Article_Editable_List);

这是从订阅获取参数的发布方法:

Meteor.publish('search_results', function (articleTitle) {
    if(articleTitle) {
      return Articles.find({articleTitle});
    }
  });

这是带有索引的集合代码:

export const Articles = new Mongo.Collection('articles');

if(Meteor.isServer) {
  Articles._ensureIndex({
    articleTitle: "text",
  });
}

这是显示搜索结果的位置:

render() {
    return (
      this.props.articles.map( (article) => {
        const dateCreated = article.createdAt.toString();
        const dateUpdated = article.updatedAt.toString();
        return (
          <div key={article._id} className="article_list_editable">
            <h1>{article.articleTitle}</h1>
            <br/>
            <h1>{article.articleAuthor}</h1>
            <div>{dateCreated}</div>
            <div>{dateUpdated}</div>
            <div>{article.articleType}</div>
          </div>
        )
      })
    )
  }

这是需要解决的错误

Error: Article_Editable_List.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

1 个答案:

答案 0 :(得分:1)

看起来你需要将地图包装在像

这样的包装中
 render() {
        return (
          <div>{
            this.props.articles.map( (article) => {
            const dateCreated = article.createdAt.toString();
            const dateUpdated = article.updatedAt.toString();
            return (
              <div key={article._id} className="article_list_editable">
                <h1>{article.articleTitle}</h1>
                <br/>
                <h1>{article.articleAuthor}</h1>
                <div>{dateCreated}</div>
                <div>{dateUpdated}</div>
                <div>{article.articleType}</div>
              </div>
            )
          }
        )
      }</div>
    )
  }

因为map返回数组,但是你只能返回单个组件,而不是数组。