I have a variable called check, which will receive a "True/false" value. Through a function called checkUserExist().
And after that i have a if statement to check on whether or not its a true or false.
The issue is that my if statement runs before "check" gets its value from the function it calls. So check remains undefined.
function createUser(){
    let userName = document.getElementById("create_username").value,
        password = document.getElementById("create_password").value;
    console.log("Create user")
    console.log("Username:", userName, "Password:", password)
    var check = checkUsersExist(userName)
    console.log("Svar fra checkUsers",check)
    if(check == "true"){
        console.log("UserName all ready exists")
    }else if(check == "false") {
        console.log("UserName is avaible")
    }else{
        console.log("An error has uccured")
    }
}
function checkUsersExist(userName){
    let headers = new Headers();
        headers.append('Content-Type', 'application/json');
    var init = {
        method: 'PUT',
        headers: headers,
        body: `{
            "userName":"${userName}"}`,
        cache: 'no-cache',
        mode: 'cors'
        };
    var request = new Request('http://localhost:3000/users/check', init);
    fetch(request)
        .then(response => 
            {
                console.log("Response", response, "Request sent to server", request)
                return response;
            })
        .catch(err => 
            {   
                console.log(err)
            });
}
I have been stuck on this for quite some time. And i am not sure how to resolve it. My fetch in checkUserExist() does get the answer i am looking for from my route
Here is my route should you be interested in looking at that as well.
module.exports = function (server) {
    server.put('/users/check', function (req, res) {
        console.log("")
        console.log("=========== NEW CHECK ============")
        console.log("")
        console.log("Kom til check på route")
        let userName = req.body.userName,
            check = "false";
        console.log("UserName received from Index.html =", userName)
        fs.readFile('./resources/users.json', 'utf8',
            function readFileCallback(err, data){
                if (err){
                    console.log(err);
                } 
                else {
                    users = JSON.parse(data); //now its a object
                    for (i = 0; i < users.table.length; i++) {
                        if(users.table[i].userName == userName){
                            console.log("===Current Check===")
                            console.log("Table =", users.table[i].userName, "Index =", userName)
                            check = "true"
                        }
                    }
                    console.log("check værdi =", check)
                    res.json(200, check)
                }
            }
        );
    });
I am working with Json files and Cookies to improve my skills with that, hence why i am not using a Database with this project.
 
     
    