An unhandled error occurred while trying to iterate through elements with {#each} in Inertia Persistent Layouts. Make sure that only array-like objects are being used

Currently, I am in the process of developing a Webapp using Laravel Inertia and Svelte.
I encountered an error when trying to use a Persistent Layout and navigating to another page using a <Link> element. The error message displayed was:

Uncaught (in promise) Error: {#each} only iterates over array-like objects when using Layouts
and it prevented redirection. This issue only occurs when clicking on the <Link> element, as directly accessing /login or / does not trigger the error. Below is the portion of code that seems to be causing this problem:

Welcome.svelte

<script context="module">
    export { default as layout } from "../Layouts/GuestLayout.svelte";
</script>

<script>
    import { Link, page } from "@inertiajs/svelte";
    import Card from "@/Components/Card.svelte";
</script>

<div class="min-h-screen">
    {#each $page.props.products as product}
        <Card productImages={product.images} productName={product['name']} productDescription={product['description']} primaryButtonText={"Buy @" + product['product_store']}></Card>
    {/each}
</div>

GuestLayout.svelte

<script>
    import { Link , page} from "@inertiajs/svelte";
    import Navbar from "@/Components/Navbar.svelte";
    export let canLogin, canRegister;
</script>

<div
    class="min-h-screen flex flex-col"
>
    <Navbar canLogin={canLogin} canRegister={canRegister} user={$page.props.auth.user}></Navbar>

    <slot />
</div>

Snippet of Navbar.svelte


        {#if canLogin}
            <div class="p-6 text-right">
                {#if $page.props.auth.user}
                    {#if canLogin}
                        <Link href={route("login")}>
                            <button class="hover:text-white">Login</button>
                        </Link>
                    {/if}
                {/if}
            </div>
        {/if}

If I remove the #each section in Welcome.svelte or eliminate the Layout usage, the issues are resolved. Any guidance or suggestions on where I may be going wrong would be highly appreciated.

Answer №1

After some investigation, I was able to find a solution to the problem at hand. Even though the $page.props.products variable is not directly used in either my GuestLayout.svelte or Navbar.svelte components, it still needs to be passed into the Inertia::render function on the /login route to prevent it from being undefined.
This adjustment was made:

        return Inertia::render('Auth/Login', [
            'canResetPassword' => Route::has('password.request'),
            'status' => session('status'),
        ]);

Transformed to this:

        return Inertia::render('Auth/Login', [
            'canResetPassword' => Route::has('password.request'),
            'status' => session('status'),
            'products' => []
        ]);

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

Navigating resolvedUrl with getServerSideProps in the newest version of NextJS - a comprehensive guide

Is there a way to obtain the pathname without using client-side rendering? I have been searching for information on how to utilize the getServerSideProps function, but so far with no luck. Initially, I attempted to employ usePathname; however, this result ...

Is it possible to utilize Angular translate to support multiple languages for individual components or modules?

Utilizing ngx-translate to switch the language of my application has proven to be quite challenging. My application consists of different languages specifically for testing purposes and is divided into separate modules with lazy loading functionality. As ...

Is there a solution for resolving the 'cannot post error' in nodejs?

Recently started using node.js I am currently working on a nodejs-experss-mongodb project and I am in the process of implementing a subscription feature that has the following specific requirements: Request Method: POST URL: localhost:8080/api/v1/users/: ...

Strategies for Refreshing a Component After Modifying Data in the Store

Having trouble updating my table component when the data in my store changes. I have a simple table using v-for as shown below: <tr v-for="d in getDatas" v-bind:key="d.id"> and buttons to navigate between pages: <button class= ...

Ensure that the HTTP GET request completes before initializing Angular using the ngOnInit lifecycle hook

Overview I am working on an Angular application that consists of three components: root, child1 (tabs.component.ts), and child2 (io.component.ts). Additionally, there is a service in place that communicates with a Tomcat server by sending get and post req ...

Adding an Icon to a Tab in Ant Design - A Step-by-Step Guide

Is there a way to include an icon before the title of each open tab? I am currently using the antd library for tab creation, which doesn't provide a direct option for adding icons. Here is my code snippet along with a link to the jsfiddle https://jsfi ...

Issue encountered while trying to load electron-tabs module and unable to generate tabs within electron framework

I've recently set up the electron-modules package in order to incorporate tabs within my Electron project. Below are snippets from the package.json, main.js, and index.html files. package.json { "name": "Backoffice", "version": "1.0.0", "descr ...

Why does the Node.js REST API keep throwing errors after I try to create just one record?

I encountered a back end issue recently. I have developed a simple API with just a post endpoint. The intention is to post data using the req.body. Oddly enough, it works perfectly fine the first time but when I attempt it again, I receive an error message ...

The specified property is not found within this type (Union type)

When working with one of multiple interfaces, I encounter an issue in Vue where it claims the property does not exist. export interface Link { to: string text?: string; } export interface Text { text: string; } export interface Html { html: string ...

Retain the previously selected option in a PHP combobox even when changing pages

Can someone please assist me with my PHP code? I am having trouble keeping my combobox unchanged after a page reload. It works fine until I change pages in my pagination. Can anyone help? THIS IS MY PHP. PHP ...

Pressing Cancel on an OctoberCMS Ajax Form still triggers submission

Currently utilizing OctoberCMS which is powered by Laravel. I have a checkbox form with Ajax functionality that utilizes Twig markup. Upon clicking the Delete button, a confirmation popup appears. However, if I choose to cancel, it still submits. This wa ...

How a Dynamic Icon Component in NextJS/React can disrupt Jest testing

Hello there! I'm a new member of this community, and usually I can find answers to my questions by searching. However, this time around, I need some help. My Current Setup I am using NextJS solely as a framework application without utilizing its API ...

Opt for the modal class over the id for improved functionality

How can I use this modal multiple times on the same page when it only works once due to the id element? Can someone assist me in making it work multiple times on the same page? I have tried the following code, but when I click the button, nothing happens. ...

Issues with Knockout functionality due to retrieval of values from an external JSON file

Attempting to manipulate formulas in a spreadsheet-like fashion using knockout appears to be causing some issues with functionality. The data in question is sourced from an external Json file. Json file { "info": [ { "Name":"Newb ...

Unit testing Jest for TypeScript files within a module or namespace

Recently, I've joined a mvc.net project that utilizes typescript on the frontend. There are numerous typescript files wrapped within module Foo {...}, with Foo representing the primary module or namespace. All these typescript files are transpiled in ...

Unable to send a form through an Express/Angular application

I have a validation function in my program that prevents the form from submitting and displays an error message if there is an error. Otherwise, it will log "Success" to the console. However, I am unable to submit the form even when there are no errors. Is ...

Include a .done() callback within a customized function

In the small class, I have included these functions: var Ajax = { // Send new entry data to database endNewEntry: function (json) { $.post("/controllers/insertEntry.ajax.php", {"json": json}); }, loadView: function (view, target, extra) { ...

What is the way to access data from a form array in Angular?

I have a form array that takes input from the user. I need to read each element of the form array individually and insert it into a database using a web service. However, I am struggling to create a method to handle this. Here is my code for the form array ...

How can I generate a bar chart in Chart.js where each group is represented by a distinct color?

Looking to build a bar chart using chart.js, but I'm struggling to create one with distinct colors for each group. Can anyone provide some guidance? https://i.sstatic.net/oAjuK.png ...

Inconsistency in the invocation of PageMethod within AJAX script manager

My AJAX call to a method in the code-behind seems to be unreliable even though I have everything set up correctly. The JavaScript function utilizes PageMethods to invoke the method in the code-behind. While most of the time it works fine, occasionally it ...