Hello everyone, I am currently attempting to post data to a JSON server using the POST method. Unfortunately, I am encountering errors. My application has buttons for actions such as follow and like. When a user clicks on the follow button, the number should increase and be saved to the JSON file. However, when a user clicks the button, I receive the following error message:
Note: I am utilizing a fake JSON server, you can find more information about it here
Error: Insert failed, duplicate id
at Function.insert (C:\Users\jelly\AppData\Roaming\npm\node_modules\json-server\node_modules\lodash-id\src\index.js:49:18)
// More lines of error messages here...
POST /statuses 500 13.873 ms - -
Below is the service code:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Status } from '../model/statuses.model';
import { Comment } from '../model/comments.model';
@Injectable({
providedIn: 'root'
})
export class UserService {
status: Status[];
constructor(private http: HttpClient) { }
// Code continuation...
Here is the TypeScript file that accompanies the service:
import { Component, OnInit } from '@angular/core';
// Other import statements...
@Component({
selector: 'app-user-profile',
templateUrl: './user-profile.component.html',
styleUrls: ['./user-profile.component.scss']
})
export class UserProfileComponent implements OnInit {
// Class properties and methods defined here...
persistStatus(status) {
this.userService.addStatus(status)
.subscribe(data => {
this.status = status;
});
}
}
JSON File Content:
{
"statuses": [
{
"id": 1,
// Additional fields of JSON data...
}
]
}
Status Model definition:
export class Status {
id: number;
// Further model details...
}
I would appreciate any guidance or feedback on what might be causing issues in my code. Thank you!