NodeJS/Knex Creating Json Response

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


NodeJS/Knex Creating Json Response



I'm currently using NodeJS with knex (Postgresql) for database stuff.



Problem:



Imagine the following two tables in database:



Table 1


PROJECT
id (pk)
name



Table 2


EMPLOYEE
id (pk)
name
project_id (fk)



I want to create a json-response to the user that looks like the following:


{
projects: [
{
id: 1,
name: 'emxample 1',
employees: [
{
id: 1,
name: 'example 1'
},
{
id: 2,
name: 'example 2'
}
]
}
]
}



and so on.



Making a query like:


knex('projects').select('projects.*', 'employee.*').join('employee', 'employee.project_id', '=', 'project.id')



And using res.json()
does not returns an array of employees.


res.json()


employees



What is the way to go to achieve that?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived