New filters on the Home Feed, take a look!
view details

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.

0
Fetch API POST method always sends a blank object no matter what I do
Post Body

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

Author
Account Strength
80%
Account Age
2 years
Verified Email
Yes
Verified Flair
No
Total Karma
9,745
Link Karma
1,834
Comment Karma
7,565
Profile updated: 4 days ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago