I have a Firestore application running (Collection: ‘students’, containing 5 documents)
One document has the id: 1
I can write with a HTML form to that collection, so i can add an individual student.
I can also get all the students in one table (Bootstrap).
But i got lost when asking for the data of an individual student.
if i open a new terminal (VS code) and give the command
$curl "http://localhost:8080/student/1"
then the requested data are shown in my terminal
{“fatherName”:”Terpstra”,”lastName”:”Gerrits”,”firstName”:”Mark”}
However: when i use this form in a HTML page
<form id="apply-form" method="post" action="http://localhost:8080/student/:id">
<label for="id">Student ID</label>
<input type="text" id="id" name="id" placeholder="....">
<button class="btn btn-primary btn-sm" type="submit">Get Data</button>
</form>
fill in the textfield: 1, and click the submit button
I get as result : Cannot POST /student/:id
If i use method="GET" in this form then the result in my browser is: http://localhost:8080/student/:id?id=1
If i type in the url: http://localhost:8080/student/1, then i get the requested data
{“fatherName”:”Terpstra”,”lastName”:”Gerrits”,”firstName”:”Mark”}
This way i could check if the data request to the database gives a result the correct data.
I do not know what is going wrong with the HTML-form, this is what i use in my application:
==== studentController.js
'use strict';
const firebase = require('../db');
const Student = require('../models/student');
const firestore = firebase.firestore();
.....
// 1 of the documents from the collection
const getStudent = async (req, res, next) => {
try {
const id = req.params.id;
const student = await firestore.collection('students').doc(id);
const data = await student.get();
//if(!data.exists) {
if(data == null) {
res.status(404).send('Student with the given ID not found');
}else {
res.send(data.data());
}
} catch (error) {
res.status(400).send(error.message);
}
}
module.exports = {
addStudent,
getAllStudents,
getStudent,
updateStudent,
deleteStudent
}
========== student.routes.js
const express = require('express');
const {addStudent,
getAllStudents,
getStudent,
updateStudent,
deleteStudent
} = require('../controllers/studentController');
// in studentController.js staan de functies uitgeschreven
const router = express.Router();
// 1 document aan de collectie toevoegen
router.post('/student', addStudent);
// alle documenten uit de collectie opvragen
router.get('/students', getAllStudents);
// 1 document uit de collectie opvragen
router.get('/student/:id', getStudent);
// 1 document uit de collectie aanpassen
router.put('/student/:id', updateStudent);
// 1 document uit de collectie verwijderen
router.delete('/student/:id', deleteStudent);
module.exports = {
routes: router
}
============ index.js
'use strict';
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const config = require('./config');
var studentRoutes = require('./routes/student-routes');
var citieRoutes = require('./routes/citie-routes');
const app = express();
app.use(express.static(__dirname + '/public'));
app.use(express.json());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
// app.use('/api', studentRoutes.routes);
app.use('/', studentRoutes.routes);
app.use('/', citieRoutes.routes);
app.listen(config.port, () => console.log('App is listening on url http://localhost:' + config.port));