how to get an id with knex and typescript?

Asked

Viewed 87 times

-1

I am creating my database with Knex for an application made in React with Typescript. The application will be a service contracting site, where a service provider registers his work and a user interested, will request a quote. I am in doubt about the time to create the budget table because I do not know how to associate the request with the chosen provider. Follow the code I’ve made so far:

import express, { request, response } from 'express';
import db from './database/connection';

const routes = express.Router();

routes.post('/services', async (request, response) => {
    const {
        name,
        avatar,
        cpf,
        email,
        cellphone,
        address,
        bio,
        category,
        speciallity,
        ranking
    } = request.body;

    const insertedWorkersIds = await db('workers').insert({
        name,
        avatar,
        cpf,
        email,
        cellphone,
        address,
        bio,
        ranking
    });

    const worker_id = insertedWorkersIds[0];

    const insertedServicesIds = await db('services').insert({
        category,
        speciallity,
        worker_id
    });

    const service_id = insertedServicesIds[0];

    return response.send();

});

routes.post('/users', async (request, response) => {
    const {
        name,
        cpf,
        email,
        address,
        cellphone
    } = request.body;

    const insertedUserId = await db('users').insert({
        name,
        cpf,
        email,
        address,
        cellphone
    });

    return response.send();

});

routes.post('/service-budget', async (request, response) => {
    const {
        day,
        time,
        work_description,
        images,
        cost,
        name,
        cpf,
        email,
        address,
        cellphone

    } = request.body;

    const insertedUserId = await db('users').insert({
        name,
        cpf,
        email,
        address,
        cellphone
    });

    const user_id = insertedUserId[0];

    const insertedServiceBudgetId = await db('service_budget').insert({
        day,
        time,
        work_description,
        images,
        cost,
        user_id
        //worker_id
    });

    return response.send();

})

export default routes;

someone knows how to catch that worker_id who is commenting on it down at service-budget?

thank you since!

1 answer

-1

The endpoint /service-budget will only be called after the user has already chosen the provider, right? If this is the case, this worker_id should be sent on the body of the post, as probably your front end received it to be able to display the services of that provider

Browser other questions tagged

You are not signed in. Login or sign up in order to post.