Retrieve the unique identifier of a single post from a JSON file within a NuxtJS project

Is there a way to retrieve the unique post id data from a JSON file in NuxtJS?

created() {
    this.fetchProductData()
},
methods: {
    fetchProductData() {
        const vueInstance = this
        this.$axios
            .get(`/json/products.json`)
            .then(function(response) {
                vueInstance.item = response.data.products
            })
            .catch((error) => console.log(error))
    }
}

https://i.stack.imgur.com/zxrIi.png

Answer №1

If you want to retrieve a specific post using its id, you can simply filter the data based on the _id field and then grab the first entry that matches (since there should only be one unique match):

methods: {
    fetchPostData() {
        const self = this;
        this.$axios
            .get(`/json/posts.json`)
            .then(function(response) {
                self.post = response.data.posts.filter((post) => {
                    return post._id === 'spekkoek-panden';
                })[0];
            })
            .catch((error) => console.log(error));
    }
}

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

Discover the web link for Vue.js 3 Composition API image online

Is there a way to import images in Vue from an array where each element includes properties that can be displayed on the page? I am able to show the properties, but I'm struggling with using the url property to display images using <img src />. ...

Using three.js to set the HTML background color as clearColor

How can I set the HTML background as clearColor using three.js? Here is the code snippet for three.js: // Setting up the environment var vWebGL = new WEBGL(); var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / ...

Angular/Ionic - How can I prevent my function from being called repeatedly while typing in an input field?

I am currently in the process of self-teaching how to construct an Angular/Ionic application. To store JSON, I am utilizing Backand and aiming to retrieve a random JSON value each time the page is refreshed. An issue I am facing is that the random call fu ...

What is the best way to display information from an array that is nested within an object retrieved from an API using

I am attempting to display data from . Using axios to fetch data from the API, I encounter an issue when trying to map through the received data as it is an array inside an object. This results in an error: TypeError: this.props.colors.map is not a funct ...

What causes the delay in loading certain CSS and JS files?

My ASP.NET MVC5 application hosted on an IIS Server has a slow-loading login page, especially after clearing the browser history. The Developer Tools Network field indicates a problem with loading page contents such as css and js files. Can you provide gui ...

Tips for resolving the "Page Not Found" error in your NextJs application

I am organizing my files in the following structure src/ ├── app/ │ ├── pages/ │ │ ├── authentication/ │ │ │ ├── SignUp/ │ │ │ │ └── page.tsx │ │ │ └── SignIn/ │ ...

The Vuetify combobox item template fails to update when coupled with Vuex

I have encountered an issue with the Vuetify combobox while using item and selection slots. The problem arises when I update my Vuex store by checking or unchecking items. Everything works fine in terms of updating for the selection slot, but if I remove a ...

Managing business logic in an observable callback in Angular with TypeScript - what's the best approach?

Attempting to fetch data and perform a task upon success using an Angular HttpClient call has led me to the following scenario: return this.http.post('api/my-route', model).subscribe( data => ( this.data = data; ...

Button to access overlay menu on my map with OpenLayers 3

I am trying to add a menu button on top of my map that, when clicked, will display a window. I have created the map div and the overlayMenu button div, but unfortunately, the button is not showing up where I want it to. I would like the button to be locate ...

Finding the Client's Private IP Address in React or Node.js: A Comprehensive Guide

Issue I am currently facing the challenge of comparing the user's private IP with the certificate's IP. Is there a method available to retrieve the user's private IP in react or node? Attempted Solution After attempting to find the user&a ...

Configure WebStorm so that node_modules is set as the library root, while simultaneously excluding it from indexing

Can WebStorm projects be configured to set the node_modules as the library root, while also excluding it from indexing? I thought I saw a project that had node_modules marked as both library root and excluded, but I'm unsure how to do this. Does anyo ...

String used in GCM instead of boolean type

I'm having an issue with receiving a message from GCM as a string instead of boolean. It seems like the problem lies within my JSON array, as I received a warning message: 04-17 00:41:04.058: W/Bundle(6702): Key alarm expected Boolean but value was a ...

Enlist partial components in express-handlebars

I'm having trouble registering partials in my app. Despite trying various solutions from other sources, nothing seems to work for me... I have set up the express handlebars as follows: import { engine } from 'express-handlebars'; const __fi ...

Obtaining the keys of an array from a JSON input

I have an array called $json that I need to work with in PHP: $json = json_decode(' {"entries":[ {"id": "29","name":"John", "age":"36"}, {"id": "30","name":"Jack", "age":"23"} ]} '); What I am trying to achieve is to create a PHP "for each" loo ...

Having trouble retrieving the .html() content from the <label> element

Check out the code below: $('.size_click').click(function () { $(this).closest('span').toggleClass('active_size'); $('.selected_sizes').append($(this).closest('label').html()); }); There are multiple ...

Navigating the authorization header of an API request in a Node environment

const authHeader = req.headers["authorization"]; I have a question that may come across as basic - why do we use ["authorization"] instead of just .authorization? After some research, I discovered it had to do with case sensitivity but ...

Python Firebase POST request encounters an unexpected token in the position 0, signaling the end of the file

I am attempting to use Firebase to send a message to a specific client. The code I am currently using for testing is as follows: import json import requests import urllib def send_message(): server = "https://fcm.googleapis.com/fcm/send" api_key ...

Tips for showcasing a photo collection using Nuxt and Strapi

Seeking advice on displaying images from a gallery page sourced from Strapi. Previous methods have not been effective in my case. My dynamic gallery page successfully retrieves collections from Strapi, but I'm struggling to showcase the images correc ...

How can I align the tags properly when inserting a tab box within a section tag?

How can I successfully insert a tab box within my section tag? I found this tab box tutorial on the website here The download link for the source code is available here: source code Below is the HTML code snippet: <!DOCTYPE html> <html> & ...

Creating a dynamically generated JavaScript array using the JSON format

I am in need of creating an array of JSON data. Here is an example: [ { "DataCategoryGroupId": "22222222-2222-2222-2222-222222222222", "AnswerOptionIds": [ "76e32546-0e26-4037-b253-823b21f6eefb", "10d02a3e-9f9f- ...