0

Error: Objects are not valid as a React child (found: object with keys {tweet, sentiment}). If you meant to render a collection of children, use an array instead.

my code: import React, { useState,useEffect } from 'react' import axios from 'axios';

export default function SentimentAnalysis() {

const [result, setResult] = useState(null); const sentimentData = async () =>{ try{ let res = await axios.get('://127.0.0.1:8000/sentiment-analysis/AAPL'); let result = res.data; setResult(result) } catch(e){ console.log(e) } }; useEffect(() => { sentimentData() }, []) console.log(result) console.log(typeof(result)) return ( <div> {result} </div> ) 

}

data to fetch: [{"tweet":"RT @DavidSchawel: Goldman highlights an amazing stat on the “haves” and “have nots” in the market today:\n\n“In aggregate, FAAMG EPS grew by…","sentiment":[0.6000000000000001,0.9]},{"tweet":"RT @DavidSchawel: What’s more amazing, that $AAPL traded at 15x earnings for so many years (and a discount to the market) or that now its m…","sentiment":[0.5333333333333333,0.6333333333333333]},{"tweet":"RT @DavidSchawel: $AAPL nearing $2T market cap. \n\nAn investor who bought $10k worth of the stock in 1993 would have sold it thirty times ov…","sentiment":[0.3,0.1]},{"tweet":"RT @DavidSchawel: Many of the biggest moneymakers over the last 5-10 years have been things that were mocked on Twitter:\n\n$AMZN “They’re no…","sentiment":[0.25,0.2833333333333333]},{"tweet":"RT @DanielEran: “You are the president of the United States of America, not just the president of conservative racists watching Fox News. W…","sentiment":[0.0,0.0]},{"tweet":"RT @DavidSchawel: I don’t think this is the start of a trend. Now if we see Braeburn Capital ($AAPL’s internal asset manager) buy then mayb…","sentiment":[0.0,0.0]},{"tweet":"RT @9to5investing: Is Lordstown Motors Stock Set to DOUBLE... AGAIN?? (DPHC)\n\n🔥🔥🔥🔥🔥\n:\n🔥🔥🔥🔥🔥\n\n$DPHC $ORCL $SPAQ $SAVA…","sentiment":[0.0,0.0]},{"tweet":"RT @DavidSchawel: With today’s rise, Berkshire’s holding in $AAPL now represents ~22% of its market cap:","sentiment":[0.0,0.0]},{"tweet":"When is $aapl event","sentiment":[0.0,0.0]},{"tweet":"@LamborghiniStar @technology $aapl stock tip #robinhood #amzn","sentiment":[0.0,0.0]},{"tweet":"RT @bespokeinvest: The 5 largest stocks at the start of 2020 have added $1.66 trillion in market cap this year. The 495 other stocks have…","sentiment":[-0.125,0.375]},{"tweet":"$AAPL's Aroon indicator reaches into Uptrend on September 14, 2020. View odds for this and other indicators:… ","sentiment":[-0.125,0.375]},{"tweet":"RT @Macromob: @DavidSchawel Guess the benchmark is so heavily concentrated by 6 stocks its prudent to hold 22% in AAPL (With ~ index weight…","sentiment":[-0.2,0.5]},{"tweet":"$TSLA\n\nhorrible customer service\n\nlowest quality fit and finish\n\nover promise under deliver\n\nfugaze earnings no pro… :","sentiment":[-0.3,0.7]}]

how to iterate through this? thanks in advance

2 Answers 2

1

This means that the item being rendered in the JSX element is an Object and not a primitive. So technically, you will need to store the tweet and the sentiment separately and then you will have to render them separately as individual primitives and not the whole object.

The easiest way would be to map through your object and get the individual primitives.

Sign up to request clarification or add additional context in comments.

Comments

0

You may forget to handle information from result. It's array object and objects are not considered as child component. Example:

return ( <div> {result.map(r => <div> {r.tweet} </div>)} </div> ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.