Can't Figure Out How to Export Index.js/Require in JavaScript/Node

谁说我不能喝 提交于 2020-12-15 01:48:26

问题


I currently have the below sample working as I expect.

Here is getSessionsLoader defined:

module.exports = {
  getSessionsLoader: () =>
    new DataLoader(async (speakerIds) => {
      return speakerIds.map(...)
    }...

This is in my server.js that I launch with node server.js

const {getSessionsLoader} = require("./dataloaders/sessionsLoader");
const server = new ApolloServer({
  context: () => {
    return {
      sessionsLoader: getSessionsLoader(),
    };
  },
});

This is the solution below I want, but it does not work.

I want to add an index.js between my server.js and sessionsLoader.js that looks like this:

const sessionsLoader = require("./sessionsLoader");
module.exports = {
  sessionsLoader,
}

Then, in my server.js, I have this:

const { sessionsLoader } = require("./dataloaders");

I get an error saying "not function" when getSessionsLoader() is called.

I feel like this is a pure JavaScript problem and I can't tell where I've gone wrong.

来源:https://stackoverflow.com/questions/64779226/cant-figure-out-how-to-export-index-js-require-in-javascript-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!