使用Flowrouter和trackerreact显示Meteor.call函数中的数据。
问题描述:
我有此代码,使用Flowrouter和trackerreact显示Meteor.call函数中的数据。
import React from 'react';
import ReactDOM from 'react-dom';
import { Meteor } from 'meteor/meteor';
import TrackerReact from 'meteor/ultimatejs:tracker-react';
const username = "test";
export default class TasksWrapper extends TrackerReact(React.Component){
constructor(){
super();
Meteor.call('getMyInfo', (error,data)=>{
if(error){
alert(error);
}else{
this.username = data.username;
}
});
}
render(){
return (
<div className="container">
<div className="row profile">
{this.username}
</div></div>
)
}
}
码的线的上方不工作。我想要的是显示用户的用户名在div中的类名称“行简介”。
当我运行这些代码时,我只能得到“测试”。它显示在页面中。我想要展示的是用户。例如,“约翰”。
我该怎么办呢?任何样品?
答
您需要通过state
存储username
时username
变化,阵营将重新呈现组件,以便:
export default class TasksWrapper extends TrackerReact(React.Component) {
constructor() {
super();
this.state = {
username: ''
};
Meteor.call('getMyInfo', (error, data) => {
if (error) {
alert(error);
} else {
this.setState({username: data.username});
}
});
}
render() {
return (
<div className="container">
<div className="row profile">
{this.state.username}
</div>
</div>
)
}
}
凡“getMyInfo”定义? – Mikkel
在服务器上。我把它放在服务器文件夹/ – JMA