Currently, I'm troubleshooting a GET request that retrieves an id and the amount of holdings a user has stored in the database. This request then calls a function to fetch updated information about the item. My goal is to also incorporate the holdings in the GET request, but I want it to be nested inside the object containing the updated information. Essentially, I aim to flatten these objects so they form an array of two objects rather than an object within an object. Below is the code I'm working with and the output I'm currently getting. I've attempted to modify the Object.assign part without success; I'm struggling to figure out how to accomplish this.
Current:
const getFullCryptoPortfolio = () =>
CryptoPortfolio.find()
.then(portfolios =>
Promise.all(portfolios.map(portfolio => getCoins(portfolio.id).then(item =>
Object.assign({}, item, {
holdings: portfolio.holdings
})))));
[
{
"0": {
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "9289.45",
"price_btc": "1.0",
"24h_volume_usd": "6536340000.0",
"market_cap_usd": "157138942782",
"available_supply": "16915850.0",
"total_supply": "16915850.0",
"max_supply": "21000000.0",
"percent_change_1h": "0.36",
"percent_change_24h": "-3.7",
"percent_change_7d": "-18.82",
"last_updated": "1520905166"
},
"holdings": 1
},
{
"0": {
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "704.491",
"price_btc": "0.0765146",
"24h_volume_usd": "1773830000.0",
"market_cap_usd": "69147117523.0",
"available_supply": "98151882.0",
"total_supply": "98151882.0",
"max_supply": null,
"percent_change_1h": "0.53",
"percent_change_24h": "-3.12",
"percent_change_7d": "-17.17",
"last_updated": "1520905152"
},
"holdings": 2
}
]
Expected Output Example:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "9289.45",
"price_btc": "1.0",
"24h_volume_usd": "6536340000.0",
"market_cap_usd": "157138942782",
"available_supply": "16915850.0",
"total_supply": "16915850.0",
"max_supply": "21000000.0",
"percent_change_1h": "0.36",
"percent_change_24h": "-3.7",
"percent_change_7d": "-18.82",
"last_updated": "1520905166",
"holdings": 1
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "704.491",
"price_btc": "0.0765146",
"24h_volume_usd": "1773830000.0",
"market_cap_usd": "69147117523.0",
"available_supply": "98151882.0",
"total_supply": "98151882.0",
"max_supply": null,
"percent_change_1h": "0.53",
"percent_change_24h": "-3.12",
"percent_change_7d": "-17.17",
"last_updated": "1520905152",
"holdings": 2
}
]