Home > Net >  TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function

Time:12-31

i have this error when i trying use the update component with my app, and i don't know, why is that error

Error:

TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
    at HttpHeaders.applyUpdate (http.mjs:244:22)
    at http.mjs:211:56
    at Array.forEach (<anonymous>)
    at HttpHeaders.init (http.mjs:211:33)
    at HttpHeaders.forEach (http.mjs:274:14)
    at Observable._subscribe (http.mjs:1811:25)
    at Observable._trySubscribe (Observable.js:37:25)
    at Observable.js:31:30
    at errorContext (errorContext.js:19:9)
    at Observable.subscribe (Observable.js:22:21)

And this is the code of my app, i use Laravel 9 in the Back-end, but the problem appears to me as if it were from the front, just in case i leave the code from back at the end

**UpdateComponent.ts: **

onSubmit(form:any){
    this._userService.update(this.token, this.user).subscribe(
      response => { 
        console.log(response)
      },
      error => {
        this.status = 'error'
        console.log(error)
      }
    )
  }

ModelUser.ts:

export class User{
    constructor(
        public id: number,
        public name: string,
        public surname: string,
        public role: string,
        public email: string,
        public password: string,
        public description: string,
        public image: string
    ){}
}

UserService.ts

update(token:any, user:any):Observable<any>{
        let json = JSON.stringify(user)
        let params = 'json='   json
        let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded').set('Authorization', token)

        return this._http.put(this.url   'user/update', params, { headers: headers })
    }

UserController/update.php

 public function update(Request $request){
        
        //Recoger los datos por POST
        $json = $request->input('json', null);
        $params_array = json_decode($json, true);

        if($checkToken && !empty($params_array)){
            //Sacar usuario identificado
            $user = $jwtAuth->checkToken($token, true);

            //Validar los datos
            $validate = \Validator::make($params_array, [
                'name' => 'required|alpha',
                'surname' => 'required|alpha',
                'email' => 'required|email|unique:users,'.$user->sub
            ]);

            //Quitar los campos que no se actualizan
            unset($params_array['id']);
            unset($params_array['role']);
            unset($params_array['password']);
            unset($params_array['created_at']);
            unset($params_array['remember_token']);

            //Actualizar el usuario en la DB
            $user_update = User::where('id', $user->sub)->update($params_array);

            //Devolver array con resultado
            $data = array(
                'code' => 200,
                'status' => 'success',
                'user' => $user,
                'changes' => $params_array
            );
        }else{
            $data = array(
                'code' => 400,
                'status' => 'error',
                'message' => 'Usuario no identificado'
            );
        }

        return response()->json($data, $data['code']);
    }

CodePudding user response:

I think your problem is in UserService.ts when you set the headers, I had a similar issue and it was because the value of the header was not a string, so in your case, you need to make sure that the type of token is string you can do that by doing .set('Authorization', String(token)) or .set('Authorization', '' token).

  • Related