带有get请求的多个数据库查询

Multiple database queries with get request

本文关键字:数据库 查询 get 请求 带有      更新时间:2023-09-26

有没有一种方法可以用一个GET请求进行多个数据库查询?

目前,我有一个返回员工数据的GET请求:

$.ajax({
    type: 'GET',
    url: '/employees',
    success: function(employees) {
        console.log(employees)
    }
});

在服务器端,它返回员工数据:

router.get('/employees', function(req, res, next) {
knex('employees').where({
    current: true
}).then(function(data) {
    res.send(data);
   });
});

但是,我想进行第二次数据库查询,以向客户端返回另一组数据。

有什么办法我能做到这一点吗?

如果您需要依赖第一个查询的输出来调用另一个查询并将其作为来自服务器的一个GET请求返回,那么有一种方法可以做到这一点:

router.get('/employees', function(req, res, next) {
knex('employees').where({
    current: true
}).then(function(data) {
    // Here, you can make another database query 
    // assuming that you need to use employees data in order to make another query
    var result = {employees : data};
    anotherModel.where({options}).then(function(childData){
      result.anotherModel = childData; 
        res.send(result);
    });
   });
});