It is essential for the object to contain a method called '[Symbol.iterator]()' which will yield an iterator upon invocation

Currently, I am facing the following error:

error TS2488: Type 'Usuario' must have a '[Symbol.iterator]()' method
that returns an iterator.

This is my code:

usuarios.reducers.ts

export interface UsuarioState {
    users: Usuario[];
    loaded: boolean;
    loading: boolean;
    error: any;
}

const initState: UsuarioState = {
    users: [],
    loaded: false,
    loading: false,
    error: null
}

export function usuariosReducer(state = initState, action: fromUsuariosActions.usuariosAcciones): UsuarioState {

    switch (action.type) {

        case fromUsuariosActions.CARGAR_USUARIOS: 
            return {
                ...state,
                loading: true
            }

        case fromUsuariosActions.CARGAR_USUARIOS_SUCCESS:
            return {
                ...state,
                loading: false,
                loaded: true,
                users:[...action.usuarios]
            }

Screenshot: https://i.sstatic.net/ZW7sj.png

I've included

"es5", "dom.iterable"
in my tsconfig.json file but it's still not functioning properly. Could you please provide insight into what might be wrong?

usuarios.actions.ts

import { Action } from '@ngrx/store';
import { Usuario } from '../../models/usuario.model';

export const CARGAR_USUARIOS = 'CARGAR_USUARIO'
export const CARGAR_USUARIOS_FAIL = 'CARGAR_USUARIO_FAIL'
export const CARGAR_USUARIOS_SUCCESS = 'CARGAR_USUARIO_SUCCESS'

export class CargarUsuarios implements Action {
    readonly type = CARGAR_USUARIOS;
}

export class CargarUsuariosFail implements Action {
    readonly type = CARGAR_USUARIOS_FAIL;

    constructor(public payload: any) {}
}

export class CargarUsuariosSuccess implements Action {
    readonly type = CARGAR_USUARIOS_SUCCESS;

    constructor(public usuarios: Usuario) {}
}

export type usuariosAcciones = CargarUsuarios |
                               CargarUsuariosFail |
                               CargarUsuariosSuccess

usuario.model.ts

export class Usuario {
    constructor(
        public id: number, 
        public first_name: string, 
        public last: string,
        public avatar: string,
    ) {
    }
}

Answer №1

In this scenario, the action.usuario holds an Object. It's worth noting that spreading an object into an Array does not have a defined behavior in the spread operator specification. Additionally, the users property is designed to receive an array of Usuario objects. To address this, you should first spread the object into another object, and then insert it as an element within the array.

...
    case fromUsuariosActions.CARGAR_USUARIOS_SUCCESS:
        return {
            ...state,
            loading: false,
            loaded: true,
            users:[{...action.usuarios}]
        }
...

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

Can you explain the meaning of `((prevState: null) => null) | null`?

Upon encountering this code snippet: import { useState } from "preact/hooks"; export default function Test() { const [state, setState] = useState(null); setState('string'); } An error is thrown: Argument of type 'string' ...

Angular2 ngIf directive issue after initial button click

I have recently started using the Angular2 Framework, so please bear with me if I make any common mistakes. I have set up two forms in separate div tags: one for logging in and another for password reset. Below the login form, there is a link that, when cl ...

The "Observable<unknown>" type is incompatible with the "Observable<{token:string; }>". Error occurred while using the tap function

I'm attempting to insert a token using the .pipe method import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; import { HttpClient } from '@angular/common ...

Generate a specific mapping for Typescript union to predefined values

In the scenario where I have a type similar to the one below and I aim to associate it with specific values during compile time, I can achieve this by: type MyType = { a: string; b: string; } const MyMapping: { [k in keyof MyType]: number } = { a: 3 ...

Incorporating Bootstrap Modal into a TypeScript-based minimalist web application

I want to incorporate the Bootstrap Modal component into my TypeScript application. Therefore, I added the necessary types: npm i @types/bootstrap After that, in my TypeScript code: import { Modal } from "bootstrap"; const myModal = new Modal(&a ...

Could it be that my function is not returning the correct type?

I've encountered an issue with my function while filtering a labelled enumeration. It seems that the function isn't returning the expected type, and I'm not sure why. function fromNameLabels<T extends string>(src: Array<[T, string] ...

The issue with the overlay not being properly shown in the Angular Material menu

After following the instructions from here to connect the menu component, I noticed that the body of the menu is simply inserted into the document's body and does not resemble a modal window. https://i.stack.imgur.com/47wlj.png The dependencies list ...

What is the most effective method for locating and modifying the initial instance of an element within a group?

In my Javascript/Typescript collection, I have the following items: [ {"order":1,"step":"abc:","status":true}, {"order":2,"step":"xyz","status":true}, {"order":3,"step":"dec","status":false}, {"order":4,"step":"pqr","status":false}, {"order":5,"step":" ...

Implementing a NextJS client component within a webpage

I am currently working with NextJS version 14 and I am in the process of creating a landing page. In one of the sections, I need to utilize the useState hook. I have specified my component as "use-client" but I am still encountering an error stating that " ...

To access a restricted selection of images stored in Firebase

Is there a way to load additional images from Firebase by clicking a button? I created a function that looks like this: onLoadMore() { if (this.all.length > 1 ) { const lastLoadedPost = _.last(this.all); const lastLoadedPostKey = lastLoadedP ...

When converting an object into a specific type, the setter of the target type is not invoked

Imagine a scenario with a class structured like this: class Individual { private _name: string; get Name(): string { return this._name; } set Name(name: string) { this._name = name; } } Upon invoking HttpClient.get<Individual>(), it retrieve ...

I would like my division to split into two halves, each with a width of 50%, and be aligned next to each other using fxLayout

<div fxLayout="row" fxLayoutAlign="space-between" style="background-color: blue;"> <div fxLayout="column" style="width: 50%;">1stOne </div> <div fxLayout="column" styl ...

Troubleshooting puppeteer error: "Property 'contentFrame' of null cannot be read"

I've encountered a bug while trying to request an API that I've implemented. After using one of the endpoints, it throws the following error, as shown below. Is there any way to handle this error? Error message: TypeError: Cannot read property ...

How can I simulate event data while testing a function that is triggered when an event is emitted from another component in Angular integration testing?

Within component A, there is a function responsible for updating the view based on data emitted from component B. The aim is to simply call this function and pass the data without integrating with component B as it would be too complicated for this particu ...

Angular TypeScript state management system

I am facing a challenge in connecting a controller to a state (using angular ui.router) where one way of writing it works, while the other does not. Successful example (with the controller registered under the module): this.$stateProvider .state(' ...

Steps to Turn Off Angular 2 Material Input Field

Please carefully review the Description below before proceeding: This is an HTML file containing code snippets: <div class="row col-md-2"> <mat-form-field appearance="outline" class="nameInput col-md-2"> <mat-label>One< ...

How to Send JS Type of 'Number' as an HTTP Response in Angular

I am currently developing an application using the MEAN stack (Mongo, Express, Angular, Node). In order to fetch the total number of entries in a collection, I am looking to create a GET route in Express that utilizes Mongo's countDocuments() method. ...

Exploring the relationship between two entities in Angular 7 using Typescript

Creating a Reactive form for user registration in Angular involves setting up the form data object separately from the viewmodel object. https://i.sstatic.net/ERlYa.png export interface RegisterViewModel { UserName: string; Email: string; Password: strin ...

Mastering the Art of Handling Postgres Error Messages for Accurate Query Execution

Currently, I am utilizing pg and node.js. The issue arises when a user logs in through the auth0 widget, as I am passing the returned email to check against my database for existing users. If the user does not exist, I am inserting their information into t ...

Maintaining scroll position in React Router v6

My website's homepage displays a user's feed. When a user clicks on one of the feed cards, they are taken to a different URL. However, when the user returns to the homepage, it automatically scrolls to the top. I am looking for a way to preserve ...