'Issue returning values with RXJS and Nestjs
I'm getting some JSON from an endpoint (I'm using @nestjs/axios for this). The issue is that the response is an Observable. This complicates things when returning values. I can't seem to get async/await to work, I'm truly out of ideas...
Here's my endpoint:
async getBalance({ address, network }: WalletDto): Promise<any> {
let nativeBalance: string
try {
this.httpService
.get<{ balance: string }>(
`https://deep-index.moralis.io/api/v2/${address}/balance?chain=${network}`,
{
headers: {
Accept: 'application/json',
'X-Api-Key': 'lol',
},
},
)
.subscribe({
next: ({ data }) => {
nativeBalance = utils.formatEther(data.balance)
},
complete: () => {
const chain = supportedChains.find(
({ name }) => name.toLowerCase() === network,
)
return {
chain,
nativeBalance,
}
},
})
} catch (error) {
// TODO handle error
}
}
I've messed around with .pipe() but don't understand what it does or how it works... Btw, supportedChains is a static array, not part of the fetch.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
