Express GET 返回空括号

Express GET returning an empty bracket

我正在尝试使用 Google Sheets API 进行 API。但是在我的应用程序中,我总是得到一个空数组作为响应。这些是我的代码:

index.routes.js

import { Router } from "express";

const router = Router();

const funciones = require("../functions/index.functions.js");

router.get("/", (req, res) => {
  res.render("index.hbs");
});

router.post("/trabajador/agregar", (req, res) => {
  console.log(req.body);

  res.send("Trabajador Agregado");
});

router.get("/trabajador/obtener", (req, res) => {
  res.send(funciones.obtenertrabajadores());
});

export default router;

index.functions.js

const {google} = require('googleapis');
const sheets = google.sheets('v4');

async function obtenertrabajadores () {
  const authClient = await authorize();
  const request = {
    // ID DE LA HOJA
    spreadsheetId: 'ID',
        
    // RANGO
    range: 'C1:C',

    // UNFORMATTED_VALUE.
    valueRenderOption: 'UNFORMATTED_VALUE', 

    // SERIAL_NUMBER
    dateTimeRenderOption: 'SERIAL_NUMBER',

    auth: authClient,
  };

  try {
    const response = (await sheets.spreadsheets.values.get(request)).data;
    return JSON.stringify(response, null, 2);
  } catch (err) {
    console.error(err);
  }
}

async function authorize() {
  let authClient = 'AuthKey';

  if (authClient == null) {
    throw Error('authentication failed');
  }

  return authClient;
}

module.exports = {
    obtenertrabajadores: obtenertrabajadores
};

如果我用控制台记录响应,代码可以工作,但是当我向 /trabajador/obtener 发出 GET 请求时,returns '{}'

我希望响应是 json,其中包含从 Google Sheet

请求中获得的数据

我想这是因为它正在返回一个承诺,但我该如何解决它?

我建议您使用函数作为中间件,因为它们往往会重复。

index.functions.js:

const {google} = require('googleapis');
const sheets = google.sheets('v4');

async function obtenertrabajadores (req, res, next) {
  const authClient = await authorize();
  const request = {
    // ID DE LA HOJA
    spreadsheetId: 'ID',
        
    // RANGO
    range: 'C1:C',

    // UNFORMATTED_VALUE.
    valueRenderOption: 'UNFORMATTED_VALUE', 

    // SERIAL_NUMBER
    dateTimeRenderOption: 'SERIAL_NUMBER',

    auth: authClient,
  };

  try {
    const response = (await sheets.spreadsheets.values.get(request)).data;
    req.response = response //Appending response to req so that you can catch it later
    return
    next()
  } catch (err) {
    console.error(err);
  }
}

async function authorize() {
  let authClient = 'AuthKey';

  if (authClient == null) {
    throw Error('authentication failed');
  }

  return authClient;
}

module.exports = obtenertrabajadores

在index.routes.js中:

router.get("/trabajador/obtener", obtenertrabajadores, (req, res) => {
  const response = req.response
  res.json(response)
});

正如@jkalandarov 所提到的,作为中间件的函数工作正常!

我只是改变了一些小东西让它工作:

index.routes.js 从“快递”导入{路由器};

const router = Router();

import obtenertrabajadores from "../functions/index.functions.js";

router.get("/", (req, res) => {
  res.render("index.hbs");
});

router.post("/trabajador/agregar", (req, res) => {
  console.log(req.body);

  res.send("Trabajador Agregado");
});

router.get("/trabajador/obtener", obtenertrabajadores, (req, res) => {
    const response = req.response
    res.json(response)
});

export default router;

index.functions.js

const {google} = require('googleapis');
const sheets = google.sheets('v4');

async function obtenertrabajadores (req, res, next) {
  const authClient = await authorize();
  const request = {
    // ID DE LA HOJA
    spreadsheetId: 'ID',
        
    // RANGO
    range: 'C1:C',

    // UNFORMATTED_VALUE.
    valueRenderOption: 'UNFORMATTED_VALUE', 

    // SERIAL_NUMBER
    dateTimeRenderOption: 'SERIAL_NUMBER',

    auth: authClient,
  };

  try {
    const response = (await sheets.spreadsheets.values.get(request)).data;
    req.response = response //Appending response to req so that you can catch it later
    return next();
  } catch (err) {
    console.error(err);
  }
}

async function authorize() {
  let authClient = 'AuthKey';

  if (authClient == null) {
    throw Error('authentication failed');
  }

  return authClient;
}

module.exports = obtenertrabajadores