I need to pass the email in the GET API request it keeps failing although i know the string works
export function userInfo1() {
let user = wixUsers.currentUser;
let userEmail;
user.getEmail()
.then( (email) => {
userEmail = email;
} );
const url = ‘https://members-7478.restdb.io/rest/user-device?q={“UserID”:"’ ;
let fullUrl = url + userEmail +‘"}’
;
return fetch(fullUrl, {
method: “get”,
mode: “cors”,
cache: “no-cache”,
headers: {
“Content-Type”: “application/json”,
“x-apikey”: “5beade40be3893357a6f2c9b”
}
})
.then( function (response) {
return response.json();
})
.then( function (json) {
return json; // This function is added to return the result in the desired format. This is required to Parse the response
})
. catch(function (error) {
console.log(‘Request failed’, error)
});
}
Here you go! You are not running the code inside the returned promise where you actually get the email as a value so that is why your code won’t work.
export function userInfo1() {
let user = wixUsers.currentUser;
let userEmail;
user.getEmail()
.then( (email) => {
// Here is the first time the email value gets stored
userEmail = email;
// Here is where all the rest of the code needs to be
const url = 'https://members-7478.restdb.io/rest/user-device?q={"UserID":"' ;
let fullUrl = url + userEmail +'"}';
return fetch(fullUrl, {
method: "get",
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
"x-apikey": "5beade40be3893357a6f2c9b"
}
})
.then(function(response) {
return response.json();
})
.then(function(json) {
return json; // This function is added to return the result in the desired format. This is required to Parse the response
})
.catch(function(error) {
console.log('Request failed', error)
});
} );
// Any code added here will not wait for the userEmail promise
// so do not try to get that value here
}
Thanks Andreas, you are, yet again a super star. My issue seems to be fundamental always around where the {} start and finish. What in your opinion is the best place to learn these basics?
@andreas-kviby Im now seeing an error in soemthing that was previosuly working fine. This would be in the frontend code when trying to parse the response
export function search_click_1(event) {
userInfo1()
.then(response => {
$w(“#Result”).text =
“UserID:” +response[0].UserID + “\n”
+“Device:” +response[0].Device;
})
}
The line of UserID is causing the uncaught promise error?
