kahmali / meteor-restivus

REST APIs for the Best of Us! - A Meteor 0.9+ package for building REST APIs https://atmospherejs.com/nimble/restivus
MIT License
544 stars 116 forks source link

When doing stream processing, how to solve Error: Cannot return null or undefined from an endpoint: #277

Open gurjit03 opened 7 years ago

gurjit03 commented 7 years ago

I am in need to do processing of stream where I need to convert my pdf data to base64 encoded string to be send to the client on his request.

Function to get pdf data

function getPdfData(rdData,url) {
      let pdfData = '';
      const doBufferProcessing = function(buffer){
        let part = buffer.toString();
        pdfData += part;
        // console.log(pdfData,"pdfData");
      }

          base64stream
            .on('data',doBufferProcessing)
            .on('end',() => {
              return {
                 data : pdfData
               }
            });
}

API endpoint

Api.addRoute('get-pdf',{authRequired : true},{
  post : function() {
    let bodyParams  = this.bodyParams;
    let url = bodyParams.url;

       const getPdfDataPrWrapper = () => {
        return new Promise((resolve,reject) => {
          let value = getPdfData(rdData, url);
          if(value) {
            resolve(value)
          }else {
            reject("error")
          }
        })
      }

      getPdfDataPrWrapper()
      .then(response => {
        console.log(response, "from the rdAnnotate Data");
        return {
          status : 'success',
          statusCode : 200,
          pdfData : response.pdfData,
          data : {
            message : "yes pdf data is return"
          }
        }
      })
      .catch(e => {
        console.log(e,"error from the rdAnnotate")
        return {
          status : 'error',
          statusCode : 500,
          data : {
            message : e
          }
        }
      })
    }
    else {
      console.log("have no rdData");
      return {
        statusCode: 404,
        status: 'error',
        data: { message: "Data for Annotations are not found"}
      }
    }
  }
this.done();
})

I want to return the response of the Promise as my response of endpoint , but it doesn't seems to work correctly, any suggestion @kahmali ?

SaydChada commented 7 years ago

Have you tried using fibers/Future to handle async response?

achirkof commented 6 years ago

Hi @gurjit03 ! I'm faced with same problem. Have you found a solution? Could you please share it? Thanks!

gurjit03 commented 6 years ago

@achirkof It's hard to recall. Since it's quite long ago. I am not in touch with the meteor restivus since then. But as far as I remember, I tried some other approach to accomplish the task.

achirkof commented 6 years ago

@gurjit03 I have solved it finally. Thank you for feedback.

gurjit03 commented 6 years ago

@achirkof then kindly share it might be helpful for people coming to this thread.