I am trying to set up a backend express API to display contact details on a contacts us page and then save the details from the form into my DB but i keep getting an error status 500 and the following error in my server
error saving data: error: relation "test" does not exist
at /Users/server/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/server/server.js:27:5 {
Here is my server
require('dotenv').config();
const express = require('express')
const app = express()
const port = 8080
const db = require('./db')
app.use(express.json())
app.use(express.urlencoded({ extended: true }));
app.get('/api/contact', (req, res) => {
res.json({
header: "Contact Us",
body: " We've been around since 2013.",
phone: "(123) 456-7890",
email: "[email protected]",
postalAddress: "1234 Property Lane, Suite 567, Citytown, ST 12345",
businessHours: "Monday - Friday, 9 AM - 5 PM"
})
})
app.post('/api/submit', async (req, res) => {
const { firstName, lastName, email, phone, message } = req.body;
try {
await db.query(
'INSERT INTO test (first_name, last_name, email, phone_number, message) VALUES ($1, $2, $3, $4, $5)',
[firstName, lastName, email, phone, message]
)
res.status(200).json({message: 'form submitted'})
} catch (error) {
console.log('error saving data:', error);
res.status(500).json({error: 'an erorr occured'})
}
})
app.listen(port, () => {
console.log(`server listening on port ${port}`);
})
DB.js file
const pg = require('pg');
require('dotenv').config();
const db = new pg.Pool({
connectionString: process.env.DATABASE_URL,
});
module.exports = db;
env.
DATABASE_URL='postgresql://localhost:5432/contact_test'
I have double checked my DB and table and no syntax error and have dropped the table and re created . I also have manually added records in psql which works but when testing through postman with the http://localhost:8080/api/submit endpoint i keep getting the same error. Can I please get some help. Thanks.