无法使用useContext钩子反应从前端向后端发送axios请求

时间:2020-06-29 01:26:55

标签: mongodb react-native axios react-hooks

我试图向后端发送请求,但收到了此错误消息

'警告:出于性能原因,此合成事件被重用。如果您看到此消息,则表示您是%s %s处于已发布/无效的综合事件中。 %s。如果必须保留原始的合成事件,请使用event.persist()。 ,访问方法timeStamp,这是一个无操作功能'

当我在我的本机应用程序上按下按钮时,我会收到错误消息。

这是我的注册文件

import React, { useState, useContext}from 'react';
import {  View, TouchableOpacity,StyleSheet} from 'react-native';
import { Card,Input,Button} from 'react-native-elements';
import {Context as AuthContext} from '../redux/authContext';

const SignUpScreen = ({navigation}) => {
    const {state, signUp } = useContext(AuthContext)
    const [firstName, setFirstName] = useState('');
    const [lastName,setLastName] = useState('');
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [telephoneNumber, setTelephoneNumber] = useState('');



    return(
        <View>
            <Card>
                <Input
                placeholder="First Name"
                value={firstName}
                onChange={setFirstName}/>
                <Input
                placeholder="Last Name"
                value={lastName}
                onChange={setLastName}/>
                <Input
                placeholder="Email Address"
                value={email}
                onChange={setEmail}
                autoCapitalize="none"
                autoCorrect={false}/>
                <Input
                placeholder="Telephone Number"
                value={telephoneNumber}
                onChange={setTelephoneNumber}
                />
                <Input
                placeholder="Password"
                value={password}
                onChange={setPassword}
                autoCapitalize="none"
                autoCorrect={false}
                secureTextEntry={true}
                />
                    <Button
                title="Enter"
                onPress={()=> signUp({firstName,lastName,email,telephoneNumber,password})}/>
            </Card>
        </View>
    )
};

SignUpScreen.navigationOptions = {
    headerShown: false
}

export default SignUpScreen;

这是我的减速器文件

import createContext from './createContext';
import mainApi from '../api/apiConnection';

const authReducer = (state, action) => {
    switch (action.type){
        default:
            return state;
    }
};

const signUp = (dispatch) => {
    return async ({firstName, lastName, email, password, telephoneNumber }) => {
        try {
            const response =  await mainApi.post('/signup',{firstName, lastName, email, password, telephoneNumber});
            console.log(response.data)
        } catch(err){
            console.log(err.message)
        }
    };
};

const login = (dispatch) => {
    return ({ email, password }) => {
    };
}

const logOut = (dispatch) => {
    return ()=> {

    }
}

export const {Provider, Context} = createContext(
    authReducer,
    {signUp,login,logOut},
    {isSignedIn: false}
);

这是我的路线文件

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const User = mongoose.model('User')


//endpoint post request
router.post('/signup', async (req,res) => {
    const {firstName, lastName, email,telephoneNumber,password} = req.body;
    //try catch to catch duplicate emails
    try{
        const user = new User({firstName, lastName, email,telephoneNumber,password});
        //saves post request to the db
        await user.save();
        //creating a token
        const token = jwt.sign({userId:user._id},'SECRET_SESSION')
        res.send({token:token})
    } catch(err){
        return res.status(422).send(err.message)
    }
});

router.post('/signin', async (req,res)=> {
    const {email, password} =req.body;
    if(!email || !password){
        return res.status(422).send({error: 'Must provide email and password'})
    }
   const user =  await User.findOne({email:email});
   if (!user){
       res.status(422).send({error: 'Invalid password or email'})
   }
   try {
       await user.comparePassword(password);
       const token = jwt.sign({userId:user._id},'SECRET_SESSION')
        res.send({token:token})
    } catch(err){
        return res.status(422).send({error: 'Invalid password or email'})
    }
});
module.exports = router;

0 个答案:

没有答案