C
C#2mo ago
Moonlit

✅ Local Post, 400 Bad Request. Can't figure out why?

Hey so I have a save button my website that uses JS to do a POST request to the server so tat I can update / add a command for the user, but I can't seem to figure out why I'm getting this error? I'll post some screenshots as a comment so they are formatted better
19 Replies
Moonlit
MoonlitOP2mo ago
So this is inside of my account.js script. Here is what the "Sending Data" log shows
{
"Command": {
"CommandName": "a",
"PermissionLevel": "Basic",
"Platforms": "Test",
"Replies": [
{
"ReplyType": "General",
"ReplyText": "c"
},
{
"ReplyType": "Viewer",
"ReplyText": "d"
},
{
"ReplyType": "Sub",
"ReplyText": "e"
}
]
},
"OriginalName": "Command Name"
}
{
"Command": {
"CommandName": "a",
"PermissionLevel": "Basic",
"Platforms": "Test",
"Replies": [
{
"ReplyType": "General",
"ReplyText": "c"
},
{
"ReplyType": "Viewer",
"ReplyText": "d"
},
{
"ReplyType": "Sub",
"ReplyText": "e"
}
]
},
"OriginalName": "Command Name"
}
No description
Moonlit
MoonlitOP2mo ago
This is my CommandsController that is meant to catch it? But I never get the console log from line 34 PS: I commented out Authorize to do a CURL request without needing to be logged in (For testing) got the same error
No description
Moonlit
MoonlitOP2mo ago
And here is my Command CS
No description
Moonlit
MoonlitOP2mo ago
You know what... It's because I don't include the UserID isn't it? But I also don't include a CommandID because the server is database is meant to create that...
greyfox
greyfox2mo ago
try it and see what happens
Moonlit
MoonlitOP2mo ago
For the CommandID, do I just make that nullable then? Can I even do that because it has the key "tag"? Sec, I'll see nope... I send CommandID and UserID with 1 and it still gives a bad request
Moonlit
MoonlitOP2mo ago
(browser console) The right side says "api/commands/save:1" so it's being sent to the right place?
No description
greyfox
greyfox2mo ago
As a sanity test, simplify your model to remove annotations and the virtual properties.
Moonlit
MoonlitOP2mo ago
Sorry that took a bit, I needed to delete a lot of code for that to work So I made a backup and tested it and yeh okay, that goes through the server and gives a 500 error now (Probably due to all the code I removed) So okay... I think I misunderstood how the Virtual properties work, it seems the missing information is due to those :Velion_Think: I honestly don't fully understand the way that they work, I don't know how they get the AppUser or the Replies... I don't quite understand how they stay updated and etc... I know that a lot of it is automated I mean I was sending Replies, so the issue must have been the AppUser How do I even do that then... Do I make AppUser nullable and then when the server gets the POST request, retrieve the AppUser and set it to the commands AppUser? I don't get it :HarryCry: I thought that would be automated if I pass in the UserID because it's a ForeignKey of Appuser
greyfox
greyfox2mo ago
You may want to use a DTO class instead of something that exposes all your internal workings.
Moonlit
MoonlitOP2mo ago
What is a DTO class?
greyfox
greyfox2mo ago
And then either build the mapping between your DTO and your Command class manually or use something like Automapper. data transfer object It's a class that only has the properties to contains the data that you want passed around your application
Moonlit
MoonlitOP2mo ago
Ohh I see... So I basically simplify the class and that is what I will send in between the clients and the server instead? And then my server will use that DTO to create the more complex one that my "Command" class is currently?
greyfox
greyfox2mo ago
yup it's a similar principle to using viewmodels - expose only what is needed, and nothing more.
Moonlit
MoonlitOP2mo ago
Okay thanks! I still don't quite understand how the virtual thing automatically handles DB relationships... If I create a Command and pass in the ID, it will automatically link the AppUser if it's done on the server exclusively? If that is the case... I feel that my Replies is probably setup wrong because I don't actually state what foreign key it ties too and each reply should have a foreign key AppUser > Command > Replies
greyfox
greyfox2mo ago
virtual in this case is for entity framework to enable lazy loading and change tracking: https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dd468057(v=vs.100)
Moonlit
MoonlitOP2mo ago
Yeh I was trying to get lazy loading to work because everytime a user adds a new command or edits one, I didn't want to need to fetch all of their data every time Okay so Replies being an ICollection is right Hmm this is pretty hard to read Is there a general naming convention for these things? Should I just call the class CommandDTO?
greyfox
greyfox2mo ago
yeah that works
Moonlit
MoonlitOP2mo ago
I honestly think the DTO might actually just fix all of this tbh... I'm just looking into it and as far as I am aware... If my UserID is set with the foreing key for AppUser. When I try to access AppUser from Command, it will load it from the DB using that foreign key? If I am understanding that correctly then I think I'm good tbh Or does it load the AppUser as soon as the UserID is set? Okay yeh EFC seems really good... So what I have gathered is as soon as I use AppUser, it will use the ForeignKey to load it the first time it is accessed and then cache it. The Replies I don't need foreign keys for, because if I try to access the Command.Replies, it will query the DB for all the Replies that have this command as their foreign key. I think I've got it Man EFT really does a lot of the heavy work... Well I think that's it? Unless there is anything you'd like to add? Thanks for the help! I really appreciate it, I was pretty lost before
Want results from more Discord servers?
Add your server