meteor-restivus icon indicating copy to clipboard operation
meteor-restivus copied to clipboard

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

Open gurjit03 opened this issue 8 years ago • 5 comments

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 ?

gurjit03 avatar May 25 '17 07:05 gurjit03

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

SaydChada avatar Jun 13 '17 10:06 SaydChada

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

achirkof avatar Jul 28 '18 21:07 achirkof

@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.

gurjit03 avatar Aug 04 '18 19:08 gurjit03

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

achirkof avatar Aug 04 '18 20:08 achirkof

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

gurjit03 avatar Aug 04 '18 20:08 gurjit03