0

I have a two part system currently. The goal of the system is to evaluate incoming data against existing box data, and then download a zip file from box.

Part 1 - I have an asynchronous function that calls the box sdk and gets file data. It then evaluates the name against a pre-existing format and returns an object.

Part 2 - Utilizes a key/value received from part 1 to create a read stream with an existing folder up on box.

The issue I am having is that part 2 is never being triggered. I look at the event listeners, or attempt to pipe the read data to a write stream and nothing happens.

I think my issue here is that I am not correctly instantiating my asynchronous functions.

Some things I know do work: I know that BoxClient is working properly. I am able to reach out to the Box SDK and pull metadata. I am also able to upload zip files to box. The upload happens in a separate function not included in this issue.

I have attempted to alternatively pull the download URL and then use async-request to pull the data. I never get any download data from the request. Same as when I attempt to utilize the read stream from the box sdk.

As discussed above this is Part 1.

async function fileLookUp(id, userName, orderNumber, planId) {
  const boxClient = auth();
  const fileIdObj = {
    fileID: id,
  };
  const expectedName = `plan.${userName}.${orderNumber}.${planId}.zip`;

  // Get all box info on given file
  const foundFile = await boxClient.files.get(fileIdObj.fileID);

  const fileName = foundFile.name;

  if (fileName !== expectedName) throw Error(messages.fileSearch);

  fileIdObj.fileName = fileName;

  return fileIdObj;
}

Part 2

async function downloadZip(obj) {
  const boxClient = auth();
  await boxClient.files.getReadStream(obj.fileID, null, (err, stream) => {
    if (err) throw Error(err);
    stream.on('data', data => console.log(data));
  });
}

I am calling the functions in jest.

test('should successfully be called', async () => {
      // 'private/plans/'
      const temp = await fileLookUp('329362776631', 'testsurgeon2', '12-15-09-0004', 'plan1');
      await downloadZip(temp);
    }, 30000);

I expect that I should get data from the read stream.

TechnicalViking
  • 669
  • 1
  • 5
  • 11
  • ` boxClient.files.getReadStream` doesn't look like it's returning a promise, so you cannot `await` it. (It will just wait for `undefined` and the async process is started in background). You might need to [promisify](https://stackoverflow.com/q/22519784/1048572) it. – Bergi Jan 21 '19 at 21:53
  • @Bergi Thank you for the comment. According to the documentation `boxClient.files.getReadStream()` should return a Promise. http://opensource.box.com/box-node-sdk/jsdoc/Files.html#getReadStream – TechnicalViking Jan 22 '19 at 13:04
  • Oh, good. You still should not use the callback then. Just `const stream = await boxClient.files.getReadStream(obj.fileID); stream.on('data', console.log);` – Bergi Jan 22 '19 at 14:15
  • @Bergi, the code above still does not return a promise . The .on seems to just get stepped over. – TechnicalViking Jan 22 '19 at 14:40
  • You mean you want to get a promise that resolves with the data? That's not what it does currently, it just creates the stream and installs a listener. – Bergi Jan 22 '19 at 14:55
  • No, the Promise is the read stream that should return the data. That stream is not returning any data. Nor is it installing any listeners. I have instantiated every listener possible for the stream, none of them get hit. – TechnicalViking Jan 22 '19 at 17:02
  • So this is not a generic promise or `async`/`await` problem but specifically the box-sdk not working? In that case, I'd recommend to file an issue at their github project. – Bergi Jan 22 '19 at 18:53

1 Answers1

1

The issue was that the test was resolving before the data stream could resolve. To fix the issue, and to gain the ability to pipe after retrieving the data, I promisified the entire function so that the test would wait for the promise to finish.

TechnicalViking
  • 669
  • 1
  • 5
  • 11