0

I am getting a returnValue from get_tlsCert.js, assigning to cert64. Now I want to use that cert64 in the lnd object below and export the lnd object. However, when I am calling this file to retrieve the lnd object exported value, I am getting an error.

How do I export lnd, please?

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "[object Array]".] { code: 'ERR_UNHANDLED_REJECTION' }

const lnService = require('ln-service');
const certFile = require('./get_tlsCert.js');
let cert64;


certFile().then(function(returnValue){
    cert64 = returnValue;
  });


const {lnd} = lnService.authenticatedLndGrpc({
  cert: cert64,
  macaroon: '123456789',
  socket: '123456789',
});

module.exports.lnd = lnd;  
Nitesh
  • 191
  • 1
  • 3
  • 7
  • Can you show us the `certFile` definition, please? It does throw an array as an exception: *`The promise rejected with the reason "[object Array]".`*. Alternatively add a `.catch(console.error)` to the promise chain. – Bergi Jun 23 '21 at 20:07
  • `….then(function(returnValue){ cert64 = returnValue; });` - that [won't work anyway](https://stackoverflow.com/q/23667086/1048572), even if it didn't throw an error. You cannot call `authenticatedLndGrpc` before the asynchronous `certFile` task is done - and you cannot export its results immediately either. – Bergi Jun 23 '21 at 20:09
  • certFile is another pulling data from another js file. looks like this. Just sending back using resolve. const lnd_directory = require('./lnd_directory.js'); const certPath = ['tls.cert']; const {join} = require('path'); const fs = require('fs'); let cert; module.exports = () => { return new Promise(function (resolve, reject){ // DOING STUFF! resolve(STUFF); }); }) } – Nitesh Jun 23 '21 at 20:22
  • What is the "*STUFF*"? That's where it gets interesting. Either use `fs.readFileSync`, or if you already are, just drop the unnecessary promise wrapper. (The alternative: make everything that depends on this module asynchronous). – Bergi Jun 23 '21 at 20:28
  • here's the full code: I am getting resolve(cert) value to the other file, if i do a console.log it prints, i think its the timing issue of the value coming back, I guess I need the code to wait for it come back before doing module.exports.lnd module.exports = () => { return new Promise(function (resolve, reject){ lnd_directory().then(function(returnValue){ cert = fs.readFileSync(join(...[returnValue].concat(certPath)), {encoding: 'base64'}); if(!cert) reject(new Error("CouldNotRetrieveCertFile")); else resolve(cert); }); }) } – Nitesh Jun 23 '21 at 20:32
  • Yes, it's a timing issue (after fixing the error you're currently getting). Notice you should avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) and just chain onto `lnd_directory`. Or maybe there's even a synchronous version of that function? – Bergi Jun 23 '21 at 20:46
  • Can you help me move from promises to async-await instead? Would that help? – Nitesh Jun 23 '21 at 20:51
  • You can only move from `.then()` syntax to `async`/`await` syntax - it's still promises, and it's still asynchronous. – Bergi Jun 23 '21 at 20:53
  • Is there a link or some guidance you can provide to switch to async/await syntax? – Nitesh Jun 23 '21 at 20:55

1 Answers1

0
    import getCert from "./get_tlsCert.js"
import lnService from 'ln-service'
import getMac from "./get_macaroon.js";
import getSocket from "./get_socket.js";
let lnd = {};

lndAuth1();

async function lndAuth1() {

  const [mac64, cert64, socket] = await Promise.all([getMac(), getCert(), getSocket()]);

  if (!mac64) {
    console.log(new Error("404-UnableToGetMacaroon"));
  }
  if (!cert64) {
    console.log(new Error("404-UnableToGetTlsCert"));
  }

  ({ lnd } = lnService.authenticatedLndGrpc({
    cert: cert64,
    macaroon: mac64,
    socket: socket,
  }));

}
export { lnd };
Nitesh
  • 191
  • 1
  • 3
  • 7