This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I am so frustrated currently. I've simplified this as much as possible to see what I am doing wrong and nothing works. Even completely isolated into it's own component it sends a blank object. Whether it is a const variable = async() => {} or async function name(){} syntax they both send blank objects. Here's my code:
import { StyleSheet, View, Text, TouchableOpacity } from "react-native";
import ROUTES from "../../constants/Routes";
export default function DebugScreen() {
// const testing = async (e) => {
// console.log('in testing')
// const item = {
// userName: "dave"
// }
// const res = await fetch(ROUTES.ANDROID_USER_USERINFO, {
// method: 'POST',
// header: { 'Content-Type': 'application/json' },
// body: JSON.stringify(item)
// })
// const data = await res.json()
// console.log(data)
// }
async function testing() {
console.log('in testing')
const res = await fetch(ROUTES.ANDROID_USER_USERINFO, {
method: 'POST',
mode: 'cors',
header: { 'Content-Type': 'application/json' },
body: JSON.stringify({ userName: "dave" })
})
const data = await res.json()
console.log(data)
}
return (
<View style={styles.container}>
<Text>Debug Stub</Text>
<TouchableOpacity onPress={() => testing()}>
<Text>Click Here To Send Info To Server</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'lightblue'
}
})
I know the route is right cause it does update the json file via json-server I have set up. But it always updates it with a blank object. I also always get back a 201 reponse code. Every stack overflow post on this just says to make sure to not have 'mode': 'no-cors' which doesn't apply to me. I've seen another parameter in the header object about something like accept type, but even with that it still sends a blank object. I can't progress on this app until I figure this out so any help would be greatly appreciated
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/reactnative...