Can't post data (Networking section)

Hey guys, i’m at the networking section and when i try to post a listing to my sever the result is always wrong… btw i didn’t implement location. Here is the handleSubmit from my code:

const handleSubmit = async (listing) => {

    const result = await listingsApi.addListings(listing);

    if (!result.ok) return alert('Error');

    alert('Success');

};

I’m not sure if the problem is there or somewhere else, maybe the course it’s outdated which i don’t belive. Any comment would be excelent, hope to get some help.

Hi meister,

Your handleSubmit looks ok. One way to debug the code above is to log the error on the terminal. Kindly update the line:

if(!result.ok) return alert(‘Error’);

to:

if(!result.ok) {
console.log(result);
alert(‘Error’);
return;
}

When you this, reload the app and confirm the status code returned (towards the end of object echoed on the terminal). When you do let me know. There is every chance that you are not passing the request header, “x-auth-token”.

Cheers,
Seun O.

Hey olowoseun i have the same error can you help me?

yarn add joi@14 solved my problem that problem happens because joi.validate is no longer supported

Got the same issue…
Here is what I get from the ‘result’ variable in ListingEditScreen.js:

{
    "config": {
        "adapter": [Function xhrAdapter], 
        "baseURL": "http://10.100.102.14:9000/api", 
        "data": {"_parts": [Array]}, 
        "env": {"FormData": null}, 
        "headers": {
            "Accept": "application/json", 
            "Content-Type": "application/json"
        }, 
        "maxBodyLength": -1, 
        "maxContentLength": -1, 
        "method": "post", 
        "onUploadProgress": [Function onUploadProgress], 
        "timeout": 0, 
        "transformRequest": [[Function transformRequest]], 
        "transformResponse": [[Function transformResponse]], 
        "transitional": {
            "clarifyTimeoutError": false, 
            "forcedJSONParsing": true, 
            "silentJSONParsing": true
        }, 
        "url": "/listings", 
        "validateStatus": [Function validateStatus], 
        "xsrfCookieName": "XSRF-TOKEN", 
        "xsrfHeaderName": "X-XSRF-TOKEN"
    }, 
    "data": null, 
    "duration": 8, 
    "headers": null, 
    "ok": false, 
    "originalError": [AxiosError: Network Error], 
    "problem": "NETWORK_ERROR", 
    "status": null
}

I am stuck on that for days… Please help

@Avivf , the result I am getting looks identical to yours. I’ve seen else where people suggest trying to run it with debugger mode turned off. That solution did not work for me but maybe its worth trying.

The debugger doesn’t work for me anyway, so I guess that’s not it…

1 Like

@Avivf Try specifying the content type in the post call, that seemed to work for me. This is what I’m returning from addListing: return client.post(endpoint, data, { headers: {'Content-Type': 'multipart/form-data'}, });

But like I mentioned before, I still get an error when I try running in debugger mode with Network Inspect enabled

3 Likes

Thanks a lot, @Alo ! It actually worked!
For people that will face this issue in the future:

  return client.post(endpoint, data, {
    headers: { "Content-Type": "multipart/form-data" },
    onUploadProgress: (progress) =>
      onUploadProgress(progress.loaded / progress.total),
  });

But the data is not really saved, right?
I mean, I don’t see it in the backend file or the Feed screen

1 Like

It won’t show up in the backend file but it should show up in your feed. Do you see the new listings when you go to localhost:9000/api/listings?

Yes, after fixing some other bugs, now it is just as you describe it

Thanks @Alo this solution work me too :+1:t4:, when I printed the error it was CLIENT_ERROR, so I thought that the error was in the request

this works for me! thanks @Alo

Is there any student, who can share subscription with me? I can pay him monthly? I cannot afford full subscription. Please anyone?