userNickName为undefined

来源:13-5 PC端登录功能模块开发

小土儿

2017-07-13

import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, Link, browserHistory} from 'react-router';

import {Row, Col} from 'antd';
import { Menu, Icon, Tabs, message, Form, Input, Button, Checkbox, Modal} from 'antd';


const FormItem = Form.Item;
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
const TabPane = Tabs.TabPane;

class PCHeader extends React.Component{
    constructor(){
        super();
        this.state = {
            current: "top",
            modalVisible: false,
            action: "login",
            hasLogined: false,
            userNickName: "",
            userid: 0
        }
    }
    setModalVisible(value){
        this.setState({modalVisible: value});
    };
    handleClick(e){
        if(e.key=="register"){
            this.setState({current: "register"});
            this.setModalVisible(true);
        } else {
            this.setState({current: e.key});
        }
    };
    handleSubmit(e){
        e.preventDefault();
        var myFetchOptions = {
            method: 'GET'
        };
        var formData = this.props.form.getFieldsValue();
        console.log(formData);
        fetch("http://newsapi.gugujiankong.com/Handler.ashx?action=" + this.state.action
            + "&username="+formData.userName+"&password="+formData.password
            +"&r_userName=" + formData.r_userName + "&r_password="
            + formData.r_password + "&r_confirmPassword="
            + formData.r_confirmPassword, myFetchOptions)
        .then(response=>response.json()).then(json=>{
            this.setState({
                userNickName: json.NickUserName,
                userid: json.UserId
            })
        });
        if(this.state.action=="login"){
            this.setState({hasLogined: true});
        }
        message.success("请求成功");
        this.setModalVisible(false);
    };

    callback(key){
        if(key==1){
            this.setState({action: 'login'});
        } else if(key==2){
            this.setState({action: 'register'});
        }
    }
    render(){
        let {getFieldProps} = this.props.form;
        const userShow = this.state.hasLogined
        ?
            <Menu.Item key="logout" class="register">
                <Button type="primary" htmlType="button">{this.state.userNickName}</Button>&nbsp;&nbsp;
                <Link target="_blank">
                    <Button type="dashed" htmlType="button">个人中心</Button>
                </Link>&nbsp;&nbsp;
                <Button type="ghost" htmlType="button">退出</Button>
            </Menu.Item>
        :
            <Menu.Item key="register" class="register">
                <Icon type="appstore"/>注册/登录
            </Menu.Item>;
        return (
            <header>
                <Row>
                    <Col span={2}></Col>
                    <Col span={4}>
                        <a href="/" class="logo">
                            <img src="./src/images/logo.png" alt="logo"/>
                            <span>React News</span>
                        </a>
                    </Col>
                    <Col span={16}>
                        <Menu mode="horizontal" onClick={this.handleClick.bind(this)} selectedKeys={[this.state.current]}>
                            <Menu.Item key="top">
                                <Icon type="appstore"/>头条
                            </Menu.Item>
                            <Menu.Item key="shehui">
                                <Icon type="appstore"/>社会
                            </Menu.Item>
                            <Menu.Item key="guonei">
                                <Icon type="appstore"/>国内
                            </Menu.Item>
                            <Menu.Item key="guoji">
                                <Icon type="appstore"/>国际
                            </Menu.Item>
                            <Menu.Item key="yule">
                                <Icon type="appstore"/>娱乐
                            </Menu.Item>
                            <Menu.Item key="tiyu">
                                <Icon type="appstore"/>体育
                            </Menu.Item>
                            <Menu.Item key="keji">
                                <Icon type="appstore"/>科技
                            </Menu.Item>
                            <Menu.Item key="shishang">
                                <Icon type="appstore"/>时尚
                            </Menu.Item>
                            {userShow}
                        </Menu>

                        <Modal title="用户中心" wrapClassName="vertical-center-modal" visible={this.state.modalVisible} onCancel={()=>this.setModalVisible(false)} onOk={()=>this.setModalVisible(false)} okText="关闭">
                            <Tabs type="card" onChange={this.callback.bind(this)}>

                                <TabPane tab="登录" key="1">
                                    <Form onSubmit={this.handleSubmit.bind(this)}>
                                        <FormItem label="账户">
                                            <Input placeholder="请输入您的账号" {...getFieldProps('r_userName')}/>
                                        </FormItem>
                                        <FormItem label="密码">
                                            <Input type="password" placeholder="请输入您的密码" {...getFieldProps('r_password')}/>
                                        </FormItem>
                                        <Button type="primary" htmlType="submit">登录</Button>
                                    </Form>
                                </TabPane>



                                <TabPane tab="注册" key="2">
                                    <Form horizontal onSubmit={this.handleSubmit.bind(this)}>
                                        <FormItem label="账户">
                                            <Input placeholder="请输入您的账号" {...getFieldProps('r_userName')}/>
                                        </FormItem>
                                        <FormItem label="密码">
                                            <Input type="password" placeholder="请输入您的密码" {...getFieldProps('r_password')}/>
                                        </FormItem>
                                        <FormItem label="确认密码">
                                            <Input type="password" placeholder="请再次输入您的密码" {...getFieldProps('r_confirmPassword')}/>
                                        </FormItem>
                                        <Button type="primary" htmlType="submit">注册</Button>
                                    </Form>
                                </TabPane>
                            </Tabs>
                        </Modal>
                    </Col>
                    <Col span={2}></Col>
                </Row>
            </header>
        );
    }
}
export default PCHeader = Form.create({})(PCHeader);

用户登录了按钮显示undefined

写回答

3回答

刚刚好hh

2017-12-04

请问这个问题你解决了吗,我也是这个问题

0
1
Cxx_Y阿7
楼主的问题是登录和注册 的字段名是一样的 所以传回来的字段为undefind
2018-01-04
共1条回复

慕粉3188243

2017-08-24

我也是这个问题,你的解决了吗

0
1
Parry
还没解决吗?还是那个错误?请直接下载我的代码你本地运行起来,然后对比看看自己哪里写错了。
2017-08-24
共1条回复

Parry

2017-07-14

你重新注册看看,API 有调整。


此慕课网 React 课程学习遇到的常见问题以及没有审核通过的补充视频(如打包后的文件压缩、单独的 webpack 2 视频教程等)

我整理成了一个大的 QA 文档,可以直接参考我博客的整理。

基本包含了所有 React 初学时遇到的各种问题的解决方案。

我的博文地址:http://blog.parryqiu.com/2017/03/09/react-q-and-a/

目前文章主要包含了以下问题的解决方案:

一些课程资源,如源代码、思维导图、后台 API 源代码等;

关于 Webpack      2.0 详细使用的 20 集免费补充视频;

webpack-dev-server 不能自动刷新的问题;

使用 Webpack 2 搭建的 React 浏览器自动刷新的项目模板,多次测试可用;

React Developer Tool 的问题;

缺少 react-html-attrs 插件;

babel-loader 没加载的错误;

fetch 的使用;

占用端口 8080 的问题;

错误 The root route must render a single      element 的处理;

如何在chrome console 中打开 paint flashing;

父组件通过 refs 获取子组件真实 DOM 节点的问题;

跳转 Route 的问题;

将 AntDesign 中的 getFieldProps 替换成 getFieldDecorator 的处理;

错误 Target container is not a DOM element      的原因及解决方案;

慕课没有发布的更新课程;

React Router 4.0 中 location undefined 的问题;

AntDesign 官方文档中的函数语法错误的解决方案;

解决课程中 React Router 版本的差异问题;

课程中 API 的地址。


0
0

结合基础与实战学习React.js 独立开发新闻头条平台

轻松入门 React 开发,React Router 4 与 Webpack 2 完美升级项目

2768 学习 · 2126 问题

查看课程