Exploring the contents of a JSON object within Next JS

I am facing an issue with accessing properties of an inner object contained within another object.

import React from 'react';
import Sidebar from "../../../components/Sidebar/Sidebar";
import {useRouter} from "next/router";

const News = (data:any) => {
    return (
        <>
            <div className="section section-main-page pt-0">
                <div className="container">
                    <div className="section section-panel">
                        <div className="section-panel-content">
                            <h1>{data.state.name}/{data.title}</h1>
                        </div>
                    </div>
                </div>
            </div>
        </>
    );
}

export default News;

export const getServerSideProps = async (context:any) =>{
    let data = await fetch(process.env.API_URL + '/' + context.query.state +'/news/' + 'test-news-article')
        .then( r => r.json() )
    console.log(data.state.name);
    return {props: {data}}
}

Although the console log for data is working, nothing prints where the title should appear in the code above. Additionally, trying to access data.state.name results in an error.

Here is the provided data:

{
    id: '1',
    title: 'Test News Article',
    summary: 'This is the summary',
    state: { id: '9', name: 'National'}
}

Answer №1

when you retrieve {properties: {information}} it needs to be accessed via

properties.information.status.title

Answer №2

By making the switch from const News = (data:any) to const News = ({data}:any), the issue was resolved.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular encountered a 405 error when trying to execute a GET request due to the method

Having trouble with implementing a new function in my application (built with angular). When I call an API (built with Lumen), it returns a 405 error when tested locally and "XMLHttpRequest cannot load Response for preflight is invalid (redirect)" when tes ...

How come my links aren't initiating jQuery click events?

Today, I decided to experiment with jQuery and encountered an issue. On my webpage, there are multiple links displayed in the format shown below: <a class="a_link" id="a_id<#>" href="#">Click me</a> The value <#> is a number gener ...

Why isn't JQuery's slideUp and fadeOut functioning correctly?

Within my Javascript code, I have the following $('ul li').click(function(){ //loops through all <li>'s inside a <ul> $('ul .clicked').removeClass('clicked'); // when an <li> is clicked, remove .cl ...

PHP REST API to handle requests and generate corresponding responses

I am currently struggling to find a solution for accurately translating the responses and requests for my PHP REST API. Here is an example of an array that I receive from the API: [{ "id": "49557a36-028b-40c6-b2d8-8095468af130", "startDate": "2020-04- ...

Updating JavaScript object property accessors

Can the property getter value of an object be modified? For instance: const autoIncrementer = (function() { let value = 0; return { incr() { value++ }, get value() { return value } }; })(); function anotherFunction ...

Create an array of various tags using the ngRepeat directive to iterate through a loop

I'm familiar with both ngRepeat and forEach, but what I really need is a combination of the two. Let me explain: In my $scope, I have a list of columns. I can display them using: <th ng-repeat="col in columns">{{ col.label }}</th> This ...

What is the correct method for asynchronously loading CSS files in web development?

On a mission to enhance my website's performance, I set out to achieve the perfect score on PageSpeed Insights. Everything was going smoothly until I encountered an issue with CSS delivery. I initially achieved a flawless result by utilizing the prel ...

A guide to crafting a fresh WordPress post with the help of the wpapi library for Node.js

After attempting to use the wpapi module to generate a post in WordPress, I encountered a puzzling issue. Despite receiving a 200 Success response, the request body was empty and no post was actually created. var wp = new WPAPI({ endpoint: 'http:/ ...

What is the best way to invoke the draw() method of Datatables with Ajax?

In my Django application, I am working on implementing the draw() method from datatables using AJAX. Both datatables and AJAX are functioning properly. However, whenever I create a new object and call the draw() method to refresh the table with the newly c ...

Struggling to retrieve information using the filter() method in MongoDB

I am currently attempting to retrieve the tasks assigned to a specific user using this setup router.get('/all', auth, async (req, res) => { try { const assignments_raw = await Assignment.find({listP: listP.indexOf(req.user.userId)}) ...

Failed to retrieve values from array following the addition of a new element

Does anyone have a solution for this problem? I recently added an element to my array using the push function, but when I tried to access the element at position 3, it wasn't defined properly processInput(inputValue: any): void { this.numOfIma ...

Tips for displaying and concealing data in Angular based on specific conditions

Within my Angular application, I have constructed a dashboard page. On this page, I have integrated a map and positioned data to appear on the right side, providing information related to the map. For example, I have created a circle with a 5km radius on t ...

Tips on how to save information from a command and retrieve it in discord.py

Currently, I am developing a Discord bot with a command called "addtoken" which prompts for a name and a contract. This command then adds the token to a dictionary. However, I am looking to persist these added tokens so that they are still available when t ...

Solution for Organizing Tables

I've sourced data from various JSON API links and displayed it in a table. Currently, my code looks like this: <script src="js/1.js"></script> <script src="js/2.js"></script> Above this code is the table structure with <t ...

Angular can invoke various controller methods based on different URLs

In my single page application, the footer features various links such as About, FAQs, Privacy, and more. As of now, I am using angular-strap modal to display a modal for each link. The code snippet looks like this: <li> <a href="#about" id= ...

Unexpected behavior observed with X Rotation in ThreeJS

Currently, I am working on a ThreeJS Demo where I have implemented the functionality to use the arrow keys for rotating the camera. Initially, everything seems to be functioning fine as I can rotate the camera up, down, left, and right successfully. Howeve ...

Implement API filtering using JavaScript

Looking to streamline the data filtering process for a car website where API queries involve parameters like brand, color, price, and fuel. https://i.sstatic.net/OYUxs.png The mock API being used is located at https://api.example.com/api/v1/it/vehicles ...

Incorporating real-time camera scanning using "react-qr-reader" in your React application

Hello, I've been working on creating a web-based QR scanner using Next.js, but I'm struggling to figure out how to implement live video for scanning QR codes. import React, { Component } from "react"; import dynamic from "next/dyna ...

Embedding content from another website onto my website

I'm in search of a solution to incorporate a part of another website into my own. Is it possible to do this using an iframe? And if so, how can I specify that only a specific section should be displayed? For instance, if the other website contains: ...

Using JavaScript and jQuery to dynamically insert an anchor tag within a script block

Hello, I am diving into the world of php and JavaScript/jQuery with a bit of HTML and CSS knowledge under my belt. I recently downloaded a template and encountered an issue that has been puzzling me as I try to navigate around it. The main problem lies in ...