HTML and API issue
Hi everyone i need serious help right now. so i have made an api with a sqlite3 database that holds values of name, rating, and id(that is auto given) and i am making a html client app that allows a user to edit this database using http methods, i have got GET and DELETE working but i am struggling severely with the POST and PUT methods and was wondering if anyone specializes in anything similar. Please help and thanks for your time
46 Replies
just as a heads up, very few people will go "yes, I will help you" as a response to an open question like this. You say you have difficulty with POST and PUT, that means you've tried something and failed. Share your code, what you tried, share where you failed, and then people can give you targeted help.
oh okay my bad ill send a picture now
if you can, share the code in a codeblock instead of a screenshot
#how-to-ask-good-questions has tips and tricks
kinda cant coz its on a vm
i keep getting a 500 internal server error
does it log anything out to the console?
yeah this is in the api console
the thing is the api works for postman
what does the console.log of req.body.game log out?
this?
no, the second line of the first screenshot
im not entirely sure tbh with you
oh, I think you're having scope issues.
change the function to an arrow function
how? sorry im still quite new to this
instead of
function (error) {
write (error) => {
this
does it work?
nah got the console error again saying reading name was undefined, a 500 internal server error
hm, actually, never mind, that wouldn't change anything. I'm not thinking right apparently
what db library are you using?
sqlite3?
you have
import db from {}
somewhere, what's between the brackets?oh its var db = new sqlite4.Database('GameRatingDB');
oh wait in my html?
no, on the server. and 4?
i ment 3
miss input
really miffed as to whats up with it coz my get and delete work perfectly
the issue seems to be that the
game
variable ends up undefined, but I don't know why exactlythats why im baffled ahaha
wait, no, it's req.body that's undefined
is this express you're using? or something else?
yeah express
why?
checking the docs
what does upload.array() do?
not entirely sure, im a student and my teachers code is layed out the same, well similar as i have different variables
is there an import / require for it?
for upload I mean
not too sure what that means aha but for the method i want the user to enter a game and a rating if thats what you mean
at the top of the file on the server, there should be a couple of lines that start with
import upload
or import * from upload
or something like const upload = require
, is there one like that?oh my bad i have this
try removing it from the app.post line, so you'd have
gamerating', function (req
? I'm just guessing now thoughnah its givent the cannot read properties error
could i be to do with that theres an id value in the database too
ah, after
var app = express();
add app.use(express.json());
NO WAY IT WORKED!!!!
wait does that mean my put will work?
that line will add req.body if you're sending a json body with fetch
it all works ahaha you have no idea how much i appreciate this
been stuck on this all day
so the way I figured this out, is by reading the docs. Something was wrong with
req
, because the error was saying req.body
was undefined. So I opened the express docs, and looked at the app.post
docs, which is the function passing in the request to the callback (the function)
There wasn't anything super useful there, but there is also a "Request" heading in the docs there. One of the things there is the documentation for req.body, https://expressjs.com/en/4x/api.html#req.body which says:
Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as express.json() or express.urlencoded().underneath it has an example of how to enable it as global middleware
The following example shows how to use body-parsing middleware to populate req.body.
ahhh i seeee
glad to have been able to help! 🙂
thank you soo much
how do i close this now lamo
lmao
just add the solved tag