服务器端代码工作正常,因为我已经用thunderclient/postman对其进行了测试,并且收到了来自外部api的正确响应(仅供参考,外部api是由退伍军人事务部管理的api)。我已经搜索了我的客户端代码,但无法找到问题所在。问题是,每次我从客户端发送 post 请求时,都会收到错误“未定义不是函数”。这是我的客户端代码(无论如何,相关部分 - 仅供参考,其中包含的个人数据是 DVA 出于开发目的提供的虚拟数据,不属于机密)...“
import React, { useState } from 'react';
import { View, Text, TextInput, TouchableOpacity, StyleSheet, ScrollView } from 'react-native';
const axios = require('axios');
import PhoneInput from 'react-native-phone-input';
export default function FormScreen({ navigation }) {
const [firstName, setFirstName] = useState('Alfredo');
const [middleName, setMiddleName] = useState('M');
const [lastName, setLastName] = useState('Armstrong');
const [gender, setGender] = useState('M');
const [birthDate, setBirthDate] = useState('1993-06-08');
const [zipCode, setZipCode] = useState('78664');
const [birthPlaceCity, setBirthPlaceCity] = useState('Johnson City');
const [homePhoneNumber, setHomePhoneNumber] = useState('555-555-5555');
const [streetAddressLine1, setStreetAddressLine1] = useState('17020 Tortoise St')
const [city, setCity] = useState('Round Rock')
const [state, setState] = useState('TX')
const [country, setCountry] = useState('USA')
const [mothersMaidenName, setMothersMaidenName] = useState('Smith')
const [birthPlaceState, setBirthPlaceState] = useState('MA')
const [birthPlaceCountry, setbirthPlaceCountry] = useState('USA')
const handleSubmit = async () => {
const formData = {
firstName,
lastName,
birthDate,
middleName,
gender,
streetAddressLine1,
city,
zipCode,
state,
country,
homePhoneNumber,
mothersMaidenName,
birthPlaceCity,
birthPlaceState,
birthPlaceCountry
};
const postURL = 'http://10.0.0.130:3500/VetVERIFICATION/VerificationPayload'
try {
const response = await axios.post(postURL, formData);
console.log('API Response:', response.data);
} catch (error) {
console.error('API Error:', error);
console.log(formData)
}
};
return (
<(some code)>
<View>
<TouchableOpacity onPress={handleSubmit} style={styles.button}>
<Text style={styles.buttonText}>Submit</Text>
</TouchableOpacity>
</View>
<View style={styles.introContainer}>
<Text style={styles.introText}>If you are having trouble verifying your
Veteran status here, please visit your VA.gov account to change or confirm
the data on file. MILVETCOM does not store any of the data you provide in this form.
</Text>
</View>
<(some closing tags)/>
);
};
同样,每次我按下“提交”按钮时,都会调用handleSubmit 函数。但它总是返回错误响应“未定义不是函数”,无论服务器端测试返回正确的响应。
我已经尝试过了
改变
const axios = require('axios');
至:
import axios from 'axios'