为什么数据没有通过我的节点 REST API 传递?

Why is Data not being passed through my Node REST API?

我有以下代码。

routes/index.js:

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

const weeklyReportsController = require('../controllers/weeklyReportsController');

router.get('/weekly_reports', weeklyReportsController);

module.exports = router;

controllers/weeklyReportsController.js:

const weeklyReportsService = require('../services/weeklyReportsService');

const weeklyReportsController = async (req, res) => {

    try {
        const data = await weeklyReportsService;
        res.json({data})
        console.log('Weekly reports controller - success');
    } catch(err) {
        console.log(err);
    }
};

module.exports = weeklyReportsController;

services/weeklyReportsService.js:

const Pool = require('pg').Pool

const pool = new Pool({connection data})
const weeklyReportsService = async () => {
    const res = await pool.query('SELECT * FROM reports', (err, results) => {
        if (err) {
            throw err;
        } else {
            console.log('Weekly reports service - success.');
        }
        return res.status(200).json(results.rows);
    });
};

module.exports = weeklyReportsService;

我通过访问 localhost:8080/api/weekly_reports 返回的只是 {} 的空 JSON 对象。我尝试在我的代码中添加一些 console.log() 方法以查看触发的内容和未触发的内容,并且我的服务的日志没有被触发。我花了几个小时试图深入研究示例代码、阅读文档,老实说只是茫然地看着我的屏幕——我只是想不通我在这里做错了什么。

有人对我做错了什么有什么建议吗?

感谢大家抽出时间,如果有什么我可以补充的,请不要犹豫,我会提供。

感谢您的帮助。

编辑:数据位于名为报告的 Postgres 数据库中,table 也称为报告。

你的方法有很多问题 以下是您实现目标的方法:

controllers/weeklyReportsController.js:

const weeklyReportsController = async (req, res) => {

    try {
        const data = await weeklyReportsService();
        res.status(200).json({data})
        console.log('Weekly reports controller - success');
    } catch(err) {
        console.log(err);
        res.status(500).json({error: err.message})
    }
};

module.exports = weeklyReportsController;

services/weeklyReportsService.js:

const Pool = require('pg').Pool

const pool = new Pool({connection data})
const weeklyReportsService = () => {
    return new Promise((resolve,reject) => {
      pool.query('SELECT * FROM reports', (err, results) => {
        if (err) reject(err);
        
        console.log('Weekly reports service - success.');
        resolve(results.rows)
      });
    })
};

module.exports = weeklyReportsService;

一些帮助您入门的链接:
https://expressjs.com/en/api.html#res

https://developer.mozilla.org/en-US/docs/Glossary/Callback_function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function