1

I have a problem. I am trying to get a single row from my database. I have done that with an API like this:

function getUserData() {
    let returnData = "";

    MyApplication.API.queryDatabase("SELECT * FROM users WHERE Id = ?", 1).done(function(data) {
        returnData = data;
    }).fail(function(reason) {
        console.log(reason);
    });
    
    return returnData;
}

And I use it like this:

let userData = getUserData();

Now this results me in the following: 0: Lastname: "Justme" Email: "just.a.test@email.com" Gender: "Male" Id: 1 Origin: "Nederland" Phonenumber: "0612345678" Firstname: "Alexander"

Now I am trying to store a single value in a variable, but when I print it, it prints:

undefined

While trying this code:

let userFirstname = userData.Firstname;
console.log(userFirstname);

How can I fix this?

isherwood
  • 58,414
  • 16
  • 114
  • 157
A. Vreeswijk
  • 822
  • 1
  • 19
  • 57
  • What does it look like when you log userData? Are the two variables in the same scope? – berkobienb Nov 30 '20 at 13:13
  • 1
    No, I am trying to return the data from the query and store it in a variable called `userData`. When I log `userData` I see an empty row, but when I log it inside the API call, it is filled. This means that `data` is filled in the query, but when I set `returnData = data` it doesn't get set. Any ideas??? – A. Vreeswijk Nov 30 '20 at 13:16

0 Answers0