henrbu
henrbu

Reputation: 208

Unsuccessfully updating postgresql columns with knex.js

facing very strange issue when on update I do not receive anything from response, no errors, no warnings, also no success message, nothing basically changes, but the request is never ending(always loading...). I'm using knex.js for queries. So basically I'm doing very simple patch request to update some properties from the table by chosen option.

I have router:

const express = require("express");
const router = express.Router();

    router
        .route("/client/:id")
        .get(getSingleClientController)
        .patch([updateClientMiddleware], updateClientController);

module.exports = router;

For testing purposes in middleware I have only next(), what means that actually it goes immediately through it. Controller basically calls the service, where's the whole logic.

Service:

const {createClientDao, getAllClientsDao, getSingleClientDao, updateClientDao} = require("../dao/adminDao");

const updateClientService = async (Request) => {
    const {id} = Request.params;

    const singleClient = await getSingleClientDao("client_id", id);
    if (singleClient.length <= 0) return {message: `Client with id ${id} does not exists`};

    const {client_name, client_vat, client_email} = Request.body;

    const clientUpdatePayload = {
        client_name: client_name ? client_name : singleClient[0].client_name,
        client_vat: client_vat ? client_vat : singleClient[0].client_vat,
        client_email: client_email ? client_email : singleClient[0].client_email
    };

    const response = await updateClientDao("client_id", id, clientUpdatePayload);
    // nothing comes here
    console.log(response);
}; 

Dao:

const updateClientDao = async (property, value, payload) => {
    return database("clients")
        .where(property, value)
        .update(payload);
};

I debugged literally everywhere, payload is fine, everything comes up, no undefined or null values. property, value on dao function comes as well. Attaching the clients table As you see there is an client_id 1, from postman I'm passing :id as 1

Upvotes: 0

Views: 96

Answers (1)

henrbu
henrbu

Reputation: 208

Seems like there were a glitch with migrations, did a rollback and working fine now. Code is correct

Upvotes: 1

Related Questions