首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Meteor-React数据未通过

Meteor-React数据未通过
EN

Stack Overflow用户
提问于 2016-10-30 03:36:26
回答 2查看 36关注 0票数 0

我刚刚开始尝试meter和react,我在网上做了一些基本的教程和一些东西,现在我正在尝试创建我自己的应用程序,但我似乎无法让它正常工作,以下是我所拥有的:

App.jsx:

代码语言:javascript
复制
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';

import AccountsUIWrapper from './AccountsUIWrapper.jsx';
import { Customers } from '../api/customers.js';

// App component - represents the whole app
class App extends Component {

  getProfile() {
        if(Meteor.userId()){
            var cx = Meteor.call('customers.getProfile', Meteor.userId());

            if(cx && cx.account){
                console.log('FOUND');
                return (
                    <div>
                        <div>Owner: {cx.owner}</div>
                        <div>Account: {cx.account}</div>
                        <div>Agents: {cx.agents}</div>
                    </div>
                )
            }else{
                console.log('LOADING..');
                return ( <div>Loading</div> );
            }
        }else{
            return (
                <div>Please sign in</div>
            )
        }
  }

  render() {
    return (
      <div className="container">
        <header>
            <AccountsUIWrapper />
            <h1>Customer Portal</h1>
        </header>

        <ul>
            {this.getProfile()}
        </ul>
      </div>
    );
  }
}


App.propTypes = {
  profile: PropTypes.array.isRequired,
  currentUser: PropTypes.object,
};

export default createContainer(() => {
  return {
    profile: [],
    currentUser: Meteor.user(),
  };
}, App);

customers.js

代码语言:javascript
复制
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';

export const Customers = new Mongo.Collection('customers');

Meteor.methods({
  'customers.getProfile'(userId) {
    check(userId, String);

    if (! this.userId) {
      throw new Meteor.Error('not-authorized');
    }

    const temp = Customers.findOne({owner: userId});
    return temp;
  },
});

我正在尝试从数据库中获取特定的客户配置文件,在教程中,他们在createContainer中有xxxx.find().fetch(),但这会带来配置文件表中的所有数据,这似乎相当有风险。

就目前而言,应用程序只是显示正在加载,没有发生其他事情。

感谢详细的帮助,因为我已经用头撞墙两天了!

EN

回答 2

Stack Overflow用户

发布于 2016-10-31 03:38:50

你的问题在这里

类App扩展了组件{

您需要使用

代码语言:javascript
复制
class About extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
    };

  }
  ....
}

我不确定extends React.Component是否与extends Component相同,但我认为你必须调用构造函数和超级(Props)来初始化所有的React功能。

票数 0
EN

Stack Overflow用户

发布于 2016-10-31 23:31:26

您还需要通知服务器发布集合

代码语言:javascript
复制
if (Meteor.isServer) {
  // This code only runs on the server
  // Only publish data you want the current user to see
  Meteor.publish(null, function() {
    return Meteor.users.find(
      { 
         _id: this.userId  
      } {
        fields: 
          Meteor.users.fieldsYouWantThemToSee

      });
  });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40322924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档