I have created an API for following a user. This method requires two parameters, which are both GUIDs. Here is the code snippet:
// Follow user
[HttpPost]
public async Task<ActionResult<Guid>> FollowUser([FromBody] Guid user_gd, Guid user2_gd)
{
if (ModelState.ErrorCount > 0)
{
return BadRequest();
}
var followedUser = await _user.FollowUser(user_gd, user2_gd);
return Ok(followedUser);
}
The manager in the API:
public async Task<bool> FollowUser(Guid user_gd, Guid user2_gd)
{
var followUserQuery =
@"
insert into userbind(gd, user_gd, followed_user_gd, date_followed)
values(@_gd, @_user_gd, @_followed_user_gd, @_date_followed)
";
await PostQuery(followUserQuery, new
{
_gd = GenerateGd(),
_user_gd = user_gd,
_followed_user_gd = user2_gd,
_date_followed = DateTime.Now
});
return true;
}
The Angular API request (service):
followUser(followed_user, user_gd): Observable<any> {
try {
return this._http.post<any>(this._apiUrl + "FollowUser", { "user2_gd": followed_user, "user_gd": user_gd }, this.httpOptions);
} catch (e) {
console.log("POST error: ", e);
}
}
The component:
followUser(gd) {
console.log(gd);
this._userService.followUser(gd, localStorage.getItem("gd")).subscribe(
res => {
console.log(res);
}
)
}
Everything seems to be functioning correctly, but I keep encountering the following error:
"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Guid' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'user2_gd', line 2, position 15."
Does anyone have a solution for this issue or has experienced the same problem before? Any help would be appreciated.