How can I ensure that a nested v-for loop in vuejs operates effectively?

I am dealing with a types variable that holds an array of objects, each containing two properties: name and content. In the content property, there is another array of objects with only one property: name.

Upon template display using {{types}}, I am seeing the following structure:

[ { "name": "Base", "content": [... ] },

{ "name": "Kilned", "content": [... ] }, 

{ "name": "Stewed", "content": [... ] },

{ "name": "Roasted/Torrefied", "content": [... ] },

 { "name": "Others", "content": [... ] } ] 

Here is a snippet of my template:

<div class="h-3/4 overflow-auto">

<div v-for="(group,index) in types">
<FermentableTypeItem
@updateModel="updateModel"
:key="index"
:type_name="group.name"
:group_name="group.name"
:state="group.state"
></FermentableTypeItem>
{{group.content}}

<FermentableTypeItem
v-for="(t,index) in group.content"
@updateModel="updateModel"
:key="index"
:type_name="t.name"
:group_name="group.name"
></FermentableTypeItem>

</div>
</div>

I am trying to add a special [1] FermentableTypeItem for each top-level element, followed by looping through the content property of these elements to add a list of regular [2] FermentableTypeItems.

Note 1: Special refers to having identical group_name and type_name properties.

Note 2: Normal indicates differing group_name and type_name properties.

While the code seems to work in displaying the FermentableTypeItems, it crashes when the variable t is utilized within the second loop, displaying an error message that 't is undefined'. Can someone assist me in resolving this issue? Perhaps it's a simple fix, but I am unable to pinpoint the problem.

Answer №1

There is no content object present. To fix this, remove any instances of null content from your response, or check for null using v-if="t != null" as shown below.

<div v-for="(t,index) in group.content" :index="index">
    <FermentableTypeItem v-if="t != null"
    @updateModel="updateModel"
    :key="index"
    :type_name="t.name"
    :group_name="group.name"
    ></FermentableTypeItem>
</div>

https://i.sstatic.net/OvuhU.png

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

Executing an external script in Nuxt after re-rendering: Best practices?

Looking for a solution in Nuxt/Vue to properly execute an external script (hosted by a third party) after the DOM has successfully rerendered on every route. The challenge arises when using a script that dynamically adds elements to the dom, causing confl ...

Incorporate a button into each row of a b-table by utilizing a template

I am attempting to include a button in each column of my table using a Vuejs template. I have successfully added the Actions column, but unfortunately the button is not displaying. It seems like I might be overlooking something. <template> <div ...

nextJS does not recognize the term 'Window'

I'm encountering the error "window is not defined" in my NextJS project. The variable isMobile determines whether the window size is less than 767.98 to handle the hamburger menu functionality. This code worked in ReactJS but seems to be causing issue ...

ReactJS form submissions failing to detect empty input values

My goal is to use react to console.log the input value. Below is the code I've created: import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component{ constructor() { super(); this.proce ...

Utilize Discord.js to send a message and patiently await your response

I am facing an issue while trying to code a Discord bot. I'm struggling to make it wait until the user inputs 'Y' or 'N'. Specifically, I am currently working on the ban command and everything seems to be functioning well until the ...

A comprehensive guide to navigating pages using AngularJS

Greetings! I've recently embarked on my Angular JS learning journey and have encountered an issue with loading content from pages. Unfortunately, I am not able to receive any content. Below are snippets of my index file and corresponding JavaScript co ...

Uh-oh! Looks like there was an issue with the AJAX response: net::

CODE: FRONT-END $(document).ready(function(){ $('.delete-post').on('click', function(){ var id = $(this).data('id'); var section = $(this).data('section'); var url = &apo ...

Only displaying sub items upon clicking the parent item in VueJS

I'm in the process of designing a navigation sidebar with main items and corresponding sub-items. I want the sub-item to be visible only when its parent item is clicked, and when a sub-item is clicked, I aim for it to stand out with a different color. ...

What is the best way to propagate a react component's props to options following an apollo-client mutation?

How can you effectively pass a react component's props to options after performing a mutation using apollo-client? I am currently utilizing react in conjunction with apollo-client. Within a specific component, I am executing a delete mutation and the ...

Here's a helpful guide on verifying the presence of a value within an array in Quasar

const myproducts = ref([]) const items = ref([ { id: 1, item: 'Vaporub 50Gm' , barcode: '123456'}, { id: 2, item: 'Herbal Cool Oil (300+100)Ml', barcode: '123456' }, { id: 3, item: 'live Oil Bp 70M ...

Vue Component not Updating even though the URL is changing

Encountering a problem where the URL updates but the page does not refresh. On the homepage, I have a list of projects. When clicking on a project, it should lead to "website.com/project/project-1" and function correctly. However, at the bottom of that p ...

During the process of adding a new template to my Angular project, I came across an issue within the core.min.js and script.js files

index.html <html class="wide wow-animation" lang="en"> <body> <app-root></app-root> <!-- Javascript--> <script src="assets/js/core.min.js"></script> <script src="assets/js/script.js"></script& ...

issues arising from missing props in laravel and vue

I'm struggling with a real-time notifications setup using Laravel, Pusher, and Vue.js. The issue I'm facing is that when trying to pass two parameters as props from the Blade template, they always end up being "undefined." Here's how it loo ...

Getting the most out of Nexmo with multiple websocket connections

I have integrated the code provided by Nexmo (shown below) into my server. However, I am facing an issue where if two callers ping my server, the binary data from the second caller also streams into the same websocket endpoint, resulting in two binary st ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

Having trouble with integrating jsPlumb with Nuxtjs and encountering the error "document is not defined"?

I've seen some similar questions, but none of the solutions have worked for me so here's my issue. I'm attempting to integrate jsPlumb into my Nuxt.js/Vue.js application. Here are the steps I've taken: npm i jsplumb --save Create a ne ...

Storing Boolean values in MongoDB with Express JS: A Step-by-Step Guide

I am encountering an issue where a Boolean value that I am trying to store in Mongodb always returns false. Below is the schema of my database: const UserSchema = new Schema({ name: String, password: { type: String, required: true }, isAdmi ...

The sluggish upload speed on Vimeo when using a library versus uploading directly

I have successfully integrated a library for uploading videos from my web app to Vimeo. Everything is working smoothly, but I am facing an issue with the upload speed. After running some tests, I noticed that the upload speed using the library is significa ...

Nextjs couldn't locate the requested page

After creating a new Next.js application, I haven't made any changes to the code yet. However, when I try to run "npm run dev," it shows me the message "ready started server on [::]:3000, url: http://localhost:3000." But when I attempt to access it, I ...

What is the best way to store a personalized configuration for a user within a Node module?

For my CLI project in Node.js utilizing commander.js, I am interested in implementing a way to store user-specific configuration settings. This will allow users to input their preferences only once during the initial usage. What would be the best approac ...