Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4807

Cant Access Variable through Promise when using async function to fetch from an api [closed]

$
0
0
async function LoadPortfolios() {        const result = await fetch("http://localhost:8080/Investor/" + globalInvestorId +"/Portfolio",            {                method: 'get',                headers: {'Accept': '*/*','Content-Type': 'application/json','accessToken': globalAccessToken.accessToken                },            })            .then(res => res.json())            .then(data => data)            .catch(err => console.log(err));            console.log(result);            return result;}async function LoadAssets(portfolioId) {    const url = "http://localhost:8080/Investor/" + globalInvestorId +"/Portfolio/Asset";    url.searchParams.append('portfolioId', portfolioId);    try {        const result = await fetch(url,            {                method: 'get',                headers: {'Accept': '*/*','Content-Type': 'application/json','accessToken': globalAccessToken.accessToken                },            })            .then(res => res.json())            .then(data => data)            .catch(err => console.log(err));        if (response.status === 200) {            console.log(result);            await renderAssets(result);        }    } catch (error) {        console.error("Error:", error);    }}async function renderPortfolios() {    let portfolios = await LoadPortfolios();    const portfoliosContainer = document.getElementById('Portfolios');    portfoliosContainer.innerHTML = ''; // Clear the container    for (let i = 0; i < portfolios.length; i++) {        const portfolio = portfolios[i];        const broker = await GetBroker(portfolio.brokerId);        console.log("In RenderPortfolios brokerUsernam:", portfolio.brokerId);        // Create a new div for the portfolio        const portfolioDiv = document.createElement('div');        portfolioDiv.classList.add('portfolio');        // Create a paragraph element to display brokerUsername        const brokerUsernameP = document.createElement('p');        brokerUsernameP.textContent = `Broker Benutzername: ${broker.username}`;        portfolioDiv.appendChild(brokerUsernameP);        // Create a paragraph element to display brokerUsername        const portfolioIdP = document.createElement('p');        portfolioIdP.textContent = `Portfolio ID: ${portfolio.portfolioId}`;        portfolioDiv.appendChild(portfolioIdP);        // Create a button Öffnen for the portfolio        const portfolioOeffnenButton = document.createElement('button');        portfolioOeffnenButton.textContent = 'Öffnen';        portfolioOeffnenButton.classList.add('openPortfoliobtn');        portfolioOeffnenButton.addEventListener('click', function() {            renderAssets(portfolio.portfolioId);        });        portfolioDiv.appendChild(portfolioOeffnenButton);        // Create a button verkaufen for the portfolio        const portfolioLoeschenButton = document.createElement('button');        portfolioLoeschenButton.textContent = 'Verkaufen';        portfolioLoeschenButton.classList.add('deletePortfoliobtn');        portfolioLoeschenButton.addEventListener('click',  function () {            DeletePortfolio(portfolio.portfolioId);        });        portfolioDiv.appendChild(portfolioLoeschenButton);        // Append the new div to the portfolios container        portfoliosContainer.appendChild(portfolioDiv);    }}

This is my Code. I want to render a bunch Portfolios into HTML. But when calling the url.append(portfolioId) in LoadAssets. The Code breaks because portfolioId is undefined. My guess is that the promise is not yet resolved in renderPortfolios. I tried both await loadPortfolios and loadPortfolios.then(etc.).

Does anyone of you has an idea? It would really help me because its for an uni project and the deadline is in like a week.

My guess is that the promise is not yet resolved in renderPortfolios. I tried both await loadPortfolios and loadPortfolios.then(etc.). But nothing did work.


Viewing all articles
Browse latest Browse all 4807

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>