OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Having Issues with Json data being undefined when trying to access it

  • Thread starter Thread starter cole palmore
  • Start date Start date
C

cole palmore

Guest
Having an issue ive been trying to solve for a while,i want my client side to send a json object over to my websocket server using fastapi for use and the server to return an different or modified json object back.This part seemed to be working whenever logging the entire returned json object would display an object in the console. The issue comes when attempting to access specific data in the object,the console will log undefined instead of the expected value. I thought the issue was with my formatting but after checking several times my json is valid i still get this issue from even just echoing the json object back to the client.

client code

Code:
const ws = new WebSocket("ws://localhost:8000/ws");

ws.onmessage = (event) => {
  data = JSON.parse(event.data) // {"a":"a","b":"b"}
  console.log(data.a) // logs undefined

<p>Having an issue ive been trying to solve for a while,i want my client side to send a json object over to my websocket server using fastapi for use and the server to return an different or modified json object back.This part seemed to be working whenever logging the entire returned json object would display an object in the console. The issue comes when attempting to access specific data in the object,the console will log undefined instead of the expected value. I thought the issue was with my formatting but after checking several times my json is valid i still get this issue from even just echoing the json object back to the client.</p>
<p>client code</p>
<pre><code>const ws = new WebSocket("ws://localhost:8000/ws");

ws.onmessage = (event) => {
data = JSON.parse(event.data) // {"a":"a","b":"b"}
console.log(data.a) // logs undefined

</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top