TechnicalViking
TechnicalViking

Reputation: 757

Box Node SDK not pulling files with readStream

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.

Upvotes: 0

Views: 749

Answers (1)

TechnicalViking
TechnicalViking

Reputation: 757

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.

Upvotes: 1

Related Questions