-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Form data is returned in as strings, not in correct TS types #6517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you please check if you are adding express json middleware or not?
Complete structure below
|
I use const app = express();
const __dirname = import.meta.dirname;
// view engine setup
app.set("views", join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(logger("dev"));
app.use(express.static(join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/oglasi", listingsRouter);
// ...
export default app; Your suggestion didn't work. |
The thing is that I don't want this data: {
"building": {
"address": "Alekse Šantića 4",
"constructedIn": "2009",
"numOfFloors": "0",
"hasParking": "on",
"hasCctv": "on",
"hasIntercom": "on"
},
"apartment": {
"location": "Grbavica",
"floor": "0",
"area": "30",
"price": "30",
"numOfRooms": "0",
"state": "Izvorno",
"heating": "Gradsko",
"equipment": "Namešten",
"items": [
"1",
"2",
"4",
"6",
"16"
]
},
"terms": {
"availableOn": "2025-05-01",
"hasDeposit": "on",
"isForStudents": "on"
},
"listing": {
"title": "Naslov",
"description": "Opis."
}
} But this: {
"building": {
"address": "Alekse Šantića 4",
"constructedIn": 2009,
"numOfFloors": 0,
"hasParking": true,
"hasGarage": false,
"hasElevator": false,
"hasCctv": true,
"hasIntercom": true
},
"apartment": {
"location": "Grbavica",
"floor": 0,
"area": 30,
"price": 30,
"numOfRooms": 0,
"state": "Izvorno",
"heating": "Gradsko",
"equipment": "Namešten",
"items": [
"1",
"2",
"4",
"6",
"16"
]
},
"terms": {
"availableOn": "2025-05-01",
"hasDeposit": true,
"isForStudents": true,
"isForWorkers": false,
"isSmokingAllowed": false,
"arePetsAllowed": false
},
"listing": {
"title": "Naslov",
"description": "Opis."
}
} So I can use the form data object in the route with Prisma: router.post("/postavka", async (req, res, next) => {
console.log(req.body);
let {
building: buildingData,
apartment: apartmentData,
terms: temrsData,
listing: listingData,
} = req.body;
buildingData = trueParse(buildingData);
apartmentData = trueParse(apartmentData);
temrsData = trueParse(temrsData);
console.log(buildingData);
let building = await prisma.building.findFirst({ where: buildingData });
if (!building)
building = await prisma.building.create({ data: buildingData });
res.status(200).json({
building: buildingData,
apartment: apartmentData,
terms: temrsData,
listing: listingData,
});
}); |
Unless you have special client-side JS code to change it, HTML
Neither of those encodings preserves information about data type (number, string, bool, Date), so effectively everything is a string. One exception is Checkboxes and radio buttons that are not checked are not sent. Checked ones are sent with value being their This has nothing to do with Express. It's just how HTML forms work. You have a couple options to solve your problems:
|
Uh oh!
There was an error while loading. Please reload this page.
I have this complicated form and I send data from it.
The route that handles the POST of this form looks like this:
The resulting data looks like this:
Everything is returned as
string
. Checkbox values should beboolean
, evenfalse
if they are unchecked. Number input values should benumber
. Date input values should beDate
. I need to pass this data into Prisma ORM functions.I know that it's a default JS thing and that it should be manually handled, but it would be a waste of time to convert all of the values, especially when you have a complex form data such as this. There are libraries
busboy
,multiparty
,formidable
,multer
, but from what I've seen, those packages are primarily used for FILE uploads. I have not seen examples of those packages being used for such forms. I've also seen examples that used onlytext
inputs. I've triedformidable
, and even this package returned form data in wrong types.I made a form parser that converts data into correct types, but it doesn't show unchecked checkbox values as false:
Environment information
Version:
Platform:
Windows: Microsoft Windows NT 10.0.26100.0 x64
Node.js version:
v22.15.0
What steps will reproduce the bug?
The text was updated successfully, but these errors were encountered: