I encountered an error while trying to use the update component in my app, and I am unsure of why this error is occurring.
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)
Additionally, for reference, here is the code snippet from my Laravel 9 backend application where the issue seems to be originating from:
**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){
//Retrieve data via POST method
$json = $request->input('json', null);
$params_array = json_decode($json, true);
if($checkToken && !empty($params_array)){
//Get identified user
$user = $jwtAuth->checkToken($token, true);
//Validate data
$validate = \Validator::make($params_array, [
'name' => 'required|alpha',
'surname' => 'required|alpha',
'email' => 'required|email|unique:users,'.$user->sub
]);
//Remove fields that should not be updated
unset($params_array['id']);
unset($params_array['role']);
unset($params_array['password']);
unset($params_array['created_at']);
unset($params_array['remember_token']);
//Update user in DB
$user_update = User::where('id', $user->sub)->update($params_array);
//Return array with result
$data = array(
'code' => 200,
'status' => 'success',
'user' => $user,
'changes' => $params_array
);
}else{
$data = array(
'code' => 400,
'status' => 'error',
'message' => 'Unidentified user'
);
}
return response()->json($data, $data['code']);
}