Can anyone explain why my program is not showing the correct alert?

userj

I have written this function which allows users to add images to the server. However, my alerts don't seem to be working. I've tried to console.log the alert messages instead of having them as alerts but that doesn't seem to work either. Any tips would be great. client.js

async function picsub(event){
    event.preventDefault();
    var image=document.getElementById('imageFile');
    const formData = new FormData();;
    formData.append('image',image.files[0]);
    let i={
        method:'POST',
        body:formData,

    }
    fetch('http://127.0.0.1:8090/pics', i).then((response) => {
        return response.text();
    }).then((response) => {
        if (response === 'success') {
            alert('pic added');
        } else {
            alert('An error has occurred');
        }
    }).catch((e) => {
        alert('An error has occurred');
    
    form.reset();

}

const form = document.getElementById("form");
form.addEventListener("submit", picsub);

server.js

const app = express();
const port = 8090;

let pictures=[];

app.post('/pics', (req, res) => {
    const pic = req.body;
    console.log(pic);
    pictures.push(pic);
    res.status(201);
});

Jack Yu

In your express, you only setup the status, but you didn't send any message from server. So, your then or catch in fetch will be executed.

then will be executed when you receive the response from server.
catch will be executed when you have failed connection between client and server.

In your case, you didn't send any response from server, fetch function will wait for response until timeout. But, you didn't setup any timeout. That means there are no response from server or any failed connection. So, your then or catch in fetch will not be executed. Unless, you shut down the server. But, browser will help you setup the default timeout, such as Chrome. It use 300 seconds as timeout.

So, you need to send message with res.end("success") like below.

app.get('/pics', function (req, res) {
    const pic = req.body;
    console.log(pic);
    pictures.push(pic);
    res.status(201).end("success");
})

And in your client.js, you should complete the bracket in the fetch function.

fetch('http://127.0.0.1:8090/pics', i).then((response) => {
    return response.text();
}).then((response) => {
    if (response === 'success') {
        alert('pic added');
    } else {
        alert('An error has occurred');
    }
}).catch((e) => {
    alert('An error has occurred');
}) // ===> here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My program doesn't print anything for the print("the"). Can anyone explain why?

Can anybody explain why this program is showing an IllegalMonitorStateException?

Why the Elevation is wrong for my coordinates, can anyone explain me

Can anyone explain, why my sorting doesn't work properly?

Can anyone explain why my Carbon setup is not retaining data?

Hello! Can anyone explain to me why my print is returning "None"?

Can anyone tell me why my alert not working

Can anyone explain how this functional program work?

Can anyone explain what is the error in the following program?

Can anyone explain the output of this program based on pointers?

Can anyone please explain the output of the following program

Can anyone please Explain the recursion logic of this program?

Can anyone tell me why my program is going in infinite Loop?

Can anyone explain why an element's content still showing after removing

can anyone explain , why this code is not working

Can anyone explain why this does not work?

Can anyone explain why this unit test is failing?

condition not working, can anyone explain why that is?

the form data is empty , can anyone explain why?

can anyone please explain why showing the error it saying require 2d but given was 1d but pandas series are 1d right can anyone please explain

Can anyone explain why these "Inappropriate blocking method call" warnings pop up from my code?

Can anyone explain why my java code multicast is not working over LAN?

Can anyone explain why my date function is giving me a wrong conversion via JS date object?

Can anyone explain why my Z-Index isn't working as expected?

Can anyone explain to me why my variable "sum" is not calculating the sum of array "arrPrice"?

Can anyone explain why my hamburger behaves like this sometimes it works fine sometimes hamburger function does not woks?

I would like to understand vue.js components and databinding with userinteractions, Can anyone correct my code and explain it to me?

Can anyone explain and trace the following multiple recursive C program?

can anyone explain <-> in typescript