0

I have function in unbound.js with the the following code

export default async function connect({ mongoose: mongoose }, URI) {
console.log('in connect');
  mongoose.connect(URI);
  mongoose.Promise = global.Promise; 
  });
}

I then have another index.js to deal with dependency injection which looks like this

module.exports = async url => {
  return await require("./unbound").default.bind(
    null,
    {
      mongoose: require("mongoose")
    },
    url
  );
};

The only thing I am doing different to plain vanilla dependency injection is to pass the URL as an argument.

When I call the export from index.js I get no response. This is confirmed by console.lognot outputting

Any guidance on how I could resolve this ?

neo-technoker
  • 369
  • 2
  • 8
  • 26
  • 1
    I think `require('./unbind')` does not give a callable object aka `function`. Have you tried to debug to see what it actually gives you? – ptdien Nov 15 '18 at 03:09
  • you are right.. i had to append it with `.default`. code is updated.. but now the function call's not even making into the code in `unbound.js` – neo-technoker Nov 15 '18 at 03:53
  • Where do you invoke the newly bound function? Because `bind` doesn't do that for you, you can try using `call` in this case. – ptdien Nov 15 '18 at 05:18
  • i am using it in app.js to establish the mongoDB connection before `express` server starts listening for requests.. tried call(). same result.. But its possible I am making a mistake. Any snippets? – neo-technoker Nov 15 '18 at 06:37
  • Sorry mate, it's really hard to know what going exactly, you should make a demo out of it. – ptdien Nov 15 '18 at 08:33
  • You should treat it as just an library you would import into your app. I am just using dependency injection index.js – neo-technoker Nov 15 '18 at 10:22
  • What I am trying to do is a pattern like what’s found here https://github.com/neotechmonk/funfunautomator/tree/master/src/fetch-politely – neo-technoker Nov 15 '18 at 10:25

2 Answers2

1

Since chat restricted, I'm gonna post the answer here instead. In this snippet, you export a function that when invoked, return another function

 module.exports = async url => {
  return await require("./unbound").default.bind(
    null,
    {
      mongoose: require("mongoose")
    },
    url
  );
};

So if you want to actually run it, you have to invoke it twice like require('./')()() for example

ptdien
  • 84
  • 4
0

As others have suggested, bind returns a bound function that you can call, it does not actually call the function - that is what .apply or .call does. @ptdien's solution is somewhat correct, but it won't work because you've forgotten to return the promise that mongoose.connect returns so your connect function returns undefined, so there is nothing the the caller to await. I.e. you need to do this:

export default function connect({ mongoose: mongoose }, url) {
  mongoose.Promise = global.Promise; 
  return mongoose.connect(url);
}

(Also note that I've removed the async keyword as that is not necessary when we are not using await - the code returns a promise already.

Also, bind will automatically forward arguments after the bound ones (i.e the url in this case), so you can simplify your code to this:

module.exports = require("./unbound").default.bind(
    null,
    {
      mongoose: require("mongoose")
    }
);

By the way, the reason you have to append .default is because you are mixing node.js requires and ES6 imports. Pick one and stick to it.

Mattias Petter Johansson
  • 1,064
  • 1
  • 15
  • 32