无极对象未返回值

问题描述 投票:-3回答:3

我试图让asset的价值:

const asset = getAssetInformation(11002);

function getAssetInformation(id) {
    return axios({
        method: 'GET',
        url: "/asset-information",
        params: {assetId: id}
    });
}

console.log(asset);

其结果是:

[object Promise]

如何获得从请求返回的值?

javascript ajax axios
3个回答
0
投票

使用asyncawait是一条可行之路 -

function axios(query) { // fake axios, for demo
  return new Promise (r =>
    setTimeout(r, 1000, { asset: query.params.assetId })
  )
}

function getAssetInformation(id) {
  return axios({
    method: 'GET',
    url: "/asset-information",
    params: {assetId: id}
  })
}

async function main() { // async
  const asset = await getAssetInformation (11022) // await
  console.log(asset)
}

main()
// 1 second later ...
// { asset: 11022 }

0
投票

您将需要使用。于是功能。

const asset = getAssetInformation(11002)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

-1
投票

公司承诺返回另一个承诺。

的方式来解压,并承诺工作就是通过。那么()函数,它会承诺返回后运行。

const asset = getAssetInformation(11002);

function getAssetInformation(id) {
    return axios({
        method: 'GET',
        url: "/asset-information",
        params: {assetId: id}
    });
}

这里的资产是一个承诺,你想用一个然后在其上得到的价值。

代替..

const asset = getAssetInformation(11002);

采用

getAssetInformation(11002)
.then((response) => {
//What you returned will be here, use response.data to get your data out.

} )
.catch((err) => {
//if there is an error returned, put behavior here. 
})
© www.soinside.com 2019 - 2024. All rights reserved.