You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js引入模块后调用News API返回空对象问题求助

我来帮你分析下问题所在,以及怎么解决这个问题:

问题根源

你的newsApi.js文件只是直接执行了API请求,但没有导出任何值。当Node.js加载这个模块时,它会执行文件里的代码,但因为没有module.exports或exports语句,所以require("../services/newsApi")得到的是默认的空对象{}——这就是你看到空对象的原因。

而且就算原文件里的API请求执行了,结果也只是在控制台打印,并没有把这个结果传递给外部调用的路由模块。

解决方案

我们需要修改newsApi.js,让它导出一个可以获取新闻数据的函数(因为API调用是异步的,最好返回Promise),然后在路由里调用这个函数来获取数据。

步骤1:修改newsApi.js,导出异步函数

把原来的代码改成导出一个函数,这个函数负责调用News API并返回结果:

const NewsAPI = require('newsapi');
const newsapi = new NewsAPI('2a59HIDDEN1bd60');

// 导出一个获取科技新闻源的异步函数
module.exports = async function fetchTechNewsSources() {
  try {
    const response = await newsapi.v2.sources({
      category: 'technology',
      language: 'en',
      country: 'us'
    });
    return response;
  } catch (err) {
    console.error('News API请求失败:', err);
    throw err; // 将错误抛出,让路由层处理
  }
};

如果你不习惯async/await,也可以直接返回Promise:

const NewsAPI = require('newsapi');
const newsapi = new NewsAPI('2a59HIDDEN1bd60');

module.exports = function fetchTechNewsSources() {
  return newsapi.v2.sources({
    category: 'technology',
    language: 'en',
    country: 'us'
  });
};

步骤2:修改Express路由,调用导出的函数

在路由文件里,我们需要调用这个导出的函数,等待异步请求完成后再把结果返回给客户端:

const passport = require("passport");
const fetchTechNewsSources = require("../services/newsApi"); // 导入导出的函数

module.exports = (app) => {
  app.get("/auth/google", passport.authenticate("google", { scope: ["profile", "email"] }));
  app.get("/auth/google/callback", passport.authenticate("google"));
  app.get('/api/current_user', (req, res) => { res.send(req.user); });
  
  // 修改这个路由,处理异步请求
  app.get('/api/newsapi', async (req, res) => {
    try {
      const newsResponse = await fetchTechNewsSources();
      console.log("-----------", newsResponse);
      res.send(newsResponse); // 将API结果返回给前端
    } catch (err) {
      res.status(500).json({ error: '获取新闻数据失败,请稍后重试' });
    }
  });
};

为什么这样能解决问题?

  • 现在newsApi.js导出了一个可复用的函数,路由模块可以主动调用它来获取数据,而不是模块加载时被动执行一次。
  • 利用async/await(或Promise)处理异步API请求,确保我们能拿到完整的响应结果后再返回给客户端,避免了异步操作导致的空值问题。

内容的提问来源于stack exchange,提问作者jsPlayer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:53:13