无法从我的本机应用程序发布请求

时间:2021-05-19 10:08:52

标签: node.js reactjs react-native react-router react-hooks

我已经创建了后端服务器并且它工作正常。我确实使用邮递员验证过 所以我把它作为我的后端网址 http://localhost:5000 现在我想在我的数据库中存储一个电话号码。 但似乎我无法与我的后端服务器建立连接

这是我面临的错误 enter image description here

更新错误

enter image description here

enter image description here

enter image description here

我正在使用 axios

import axios from 'axios';


const instance = axios.create({
    baseURL: 'http://localhost:5000',
});

export default instance;

这就是我试图发布我的数据的方式 错误是在我尝试连接到我的服务器时从这一行看到的 const resp = await lifeApi.post('/signup', { data });

import React, { useState, useEffect } from 'react';
import { TextInput, View, TouchableOpacity, Text } from 'react-native';
import { StyleSheet } from 'react-native';
import auth from '@react-native-firebase/auth';
import lifeApi from '../../api/life';

//const User = require('../../../New folder/aol-backend/src/models/User');

const VerifyOTP = ({ route: { params: { phoneNumber } }, navigation }) => {
    // If null, no SMS has been sent
    //const [confirm, setConfirm] = useState(true);
    const [otp, setOtp] = useState('');
    //const [otpArray, setOtpArray] = useState(['', '', '', '']);
    const [confirm, setConfirm] = useState(null);
    //const User = require('../../../../folder/src/models/User');
    useEffect(() => {
        signInWithPhoneNumber();
    }, []);

    async function signInWithPhoneNumber() {
        try {
            const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
            setConfirm(confirmation);
        } catch (e) {
            alert(JSON.stringify(e));
        }
    }
//==========================this is where i am trying to post request ==================================================
    async function confirmCode() {
        try {
            const code = otp;
            const response = await confirm.confirm(code);

            //================
            const data = { phone: response.user._user.phoneNumber };
            console.log(JSON.stringify(data));
            try {
                const resp = await lifeApi.post('/signup', { data });
                console.log(resp);
            } catch (err) {
                console.log(err)
            }
            //================
            console.log(response.user._user.phoneNumber);
            navigation.navigate('Home');
        } catch (e) {
            alert(JSON.stringify(e));
        }
    }
//=============================================================================
    return (
        <View>
            <TextInput
                style={style.display}
                value={otp}
                onChangeText={text => setOtp(text)}
                keyboardType="number-pad"
            />
            <TouchableOpacity
                onPress={() => {
                    confirmCode();
                }}
            >
                <Text>
                    submit
                </Text>
            </TouchableOpacity>
        </View>
    );
}

const style = StyleSheet.create({
    display: {
        borderColor: 'black',
        borderWidth: 2,
    }
});
export default VerifyOTP;

/**
          *  if (response) {
             await new User({
                 phone: response.user._user.phoneNumber
             }).save();
          */

如果您需要有关我的问题的更多信息,请告诉 请帮忙...

1 个答案:

答案 0 :(得分:0)

  1. 运行服务器应用程序且移动设备应连接到同一网络的本地计算机(笔记本电脑/台式机)。

    • 通过 Wi-Fi 将您的移动设备连接到本地网络。
    • 将您的笔记本电脑也连接到同一网络。 (服务器在笔记本电脑上运行)
  2. 通过在终端 (ifconfig/ipconfig) 中运行命令来检查笔记本电脑的 IP

  3. 在移动应用程序中设置上面的 IP 地址要测试哪个应用程序。

     const instance = axios.create({baseURL: 'http://192.168.0.29:5000'}); //IP:Port number