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
Jochem
Jochem•12mo ago
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.
alarminggg
alarmingggOP•12mo ago
oh okay my bad ill send a picture now
Jochem
Jochem•12mo ago
if you can, share the code in a codeblock instead of a screenshot #how-to-ask-good-questions has tips and tricks
alarminggg
alarmingggOP•12mo ago
kinda cant coz its on a vm
alarminggg
alarmingggOP•12mo ago
No description
No description
No description
alarminggg
alarmingggOP•12mo ago
i keep getting a 500 internal server error
Jochem
Jochem•12mo ago
does it log anything out to the console?
alarminggg
alarmingggOP•12mo ago
yeah this is in the api console
No description
alarminggg
alarmingggOP•12mo ago
the thing is the api works for postman
Jochem
Jochem•12mo ago
what does the console.log of req.body.game log out?
alarminggg
alarmingggOP•12mo ago
this?
No description
Jochem
Jochem•12mo ago
no, the second line of the first screenshot
alarminggg
alarmingggOP•12mo ago
im not entirely sure tbh with you
Jochem
Jochem•12mo ago
oh, I think you're having scope issues. change the function to an arrow function
alarminggg
alarmingggOP•12mo ago
how? sorry im still quite new to this
Jochem
Jochem•12mo ago
instead of function (error) { write (error) => {
alarminggg
alarmingggOP•12mo ago
this
No description
Jochem
Jochem•12mo ago
does it work?
alarminggg
alarmingggOP•12mo ago
nah got the console error again saying reading name was undefined, a 500 internal server error
Jochem
Jochem•12mo ago
hm, actually, never mind, that wouldn't change anything. I'm not thinking right apparently what db library are you using?
alarminggg
alarmingggOP•12mo ago
sqlite3?
Jochem
Jochem•12mo ago
you have import db from {} somewhere, what's between the brackets?
alarminggg
alarmingggOP•12mo ago
oh its var db = new sqlite4.Database('GameRatingDB'); oh wait in my html?
Jochem
Jochem•12mo ago
no, on the server. and 4?
alarminggg
alarmingggOP•12mo ago
i ment 3 miss input really miffed as to whats up with it coz my get and delete work perfectly
Jochem
Jochem•12mo ago
the issue seems to be that the game variable ends up undefined, but I don't know why exactly
alarminggg
alarmingggOP•12mo ago
thats why im baffled ahaha
Jochem
Jochem•12mo ago
wait, no, it's req.body that's undefined is this express you're using? or something else?
alarminggg
alarmingggOP•12mo ago
yeah express why?
Jochem
Jochem•12mo ago
checking the docs what does upload.array() do?
alarminggg
alarmingggOP•12mo ago
not entirely sure, im a student and my teachers code is layed out the same, well similar as i have different variables
Jochem
Jochem•12mo ago
is there an import / require for it? for upload I mean
alarminggg
alarmingggOP•12mo ago
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
Jochem
Jochem•12mo ago
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?
alarminggg
alarmingggOP•12mo ago
oh my bad i have this
No description
Jochem
Jochem•12mo ago
try removing it from the app.post line, so you'd have gamerating', function (req? I'm just guessing now though
alarminggg
alarmingggOP•12mo ago
nah its givent the cannot read properties error could i be to do with that theres an id value in the database too
Jochem
Jochem•12mo ago
ah, after var app = express(); add app.use(express.json());
alarminggg
alarmingggOP•12mo ago
NO WAY IT WORKED!!!! wait does that mean my put will work?
Jochem
Jochem•12mo ago
that line will add req.body if you're sending a json body with fetch
alarminggg
alarmingggOP•12mo ago
it all works ahaha you have no idea how much i appreciate this been stuck on this all day
Jochem
Jochem•12mo ago
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.
var express = require('express')

var app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.post('/profile', function (req, res, next) {
console.log(req.body)
res.json(req.body)
})
var express = require('express')

var app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.post('/profile', function (req, res, next) {
console.log(req.body)
res.json(req.body)
})
alarminggg
alarmingggOP•12mo ago
ahhh i seeee
Jochem
Jochem•12mo ago
glad to have been able to help! 🙂
alarminggg
alarmingggOP•12mo ago
thank you soo much how do i close this now lamo lmao
Jochem
Jochem•12mo ago
just add the solved tag
Want results from more Discord servers?
Add your server