RendezYT
RendezYT

Reputation: 61

How do you do a PATCH request on data seeded into a PostgreSQL database in JavaScript

In summary I have a bird database and I am trying to do a PATCH request on seeded data I have added using the node-postgres Pool object to create a database connection with a pool of clients. I am trying to do something simple here where I want to modify the email address of the bird watcher.

Note: I know there are no error handlers/catch blocks; I am going to add this later; I want to get the basics done first

part of my app.js looks like this


//app.js
const {modifyBirdWatcherEmail} = require('./controllers/bwatchers.controller.js')

...other endpoints and HTTP request methods (GET and POST have been left out here as they are working fine)
const express = require('express');
const app = express();
app.use(express.json())
app.patch('/api/birdwatchers/:bw_id',modifyBirdWatcherEmail)
module.exports = app

Next is one of my controllers for birdwatchers below:

//bwatchers.controller.js
const {updateBirdWatcherEmail} = require('../models/b_watchers.models.js')
exports.modifyBirdWatcherEmail = (req,res) =>{
    let {bw_id} = req.params
    const {email_address} = req.body
    bw_id *= 1;
    updateBirdWatcherEmail(bw_id,email_address).then((updatedEmail)=>{
        return res.status(200).send({updatedEmail})
    })
}

Now here is the model file that interacts with the database to PATCH the data using the UPDATE PostgreSQL command. I am using pg-format here to insert birdwatcher id via an array


//b_watchers.models.js

exports.updateBirdWatcherEmail = (bw_id,email_address) =>{
  const queryStr = `UPDATE birdwatchers
        SET email_address = ${email_address}
        WHERE bw_id = $1
        RETURNING 
        *;
        `
  return db.query(queryStr,[bw_id]).then((result)=>result.rows[0])
}

Here is the table schema for birdwatchers below:

function createbirdWatchers_rel(){//birdwatchers table
    return db.query(`
    CREATE TABLE birdwatchers (
        bw_id SERIAL PRIMARY KEY,
        formal_title VARCHAR(4) NOT NULL,
        first_name VARCHAR(30) NOT NULL,
        last_name VARCHAR(45) NOT NULL,
        age INT NOT NULL,
        email_address VARCHAR (35) UNIQUE 
    )
    `)
}

Also here is the part of the data that gets seeded into the postgresql database.

const birdwatchersD = [
  {formal_title: "Mr",
   first_name: "James",
   last_name: "Sunderland",
   age: 22,
   email_address: "[email protected]"
  },
  {formal_title: "Dr",
   first_name: "Lethart",
   last_name: "Attenborough",
   age: 63,
   email_address: "[email protected]"
  }
]

Issue: When I run the create the database via the terminal, run the seeding command and start nodemon the server starts up fine but when I try to do a patch request on Insomnia or Postman to modify the email I get the below error in the terminal (how to fix it):

enter image description here

error: column "jamessun877" does not exist
    at /home/username/Programs/programs/projects/birdwatchersrepo/node_modules/pg-pool/index.js:45:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  length: 111,
  severity: 'ERROR',
  code: '42703',
  detail: undefined,
  hint: undefined,
  position: '49',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '3638',
  routine: 'errorMissingColumn'
}

Node.js v20.7.0
[nodemon] app crashed - waiting for file changes before starting...

Upvotes: 0

Views: 276

Answers (0)

Related Questions