meteor-restivus
meteor-restivus copied to clipboard
When doing stream processing, how to solve Error: Cannot return null or undefined from an endpoint:
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 ?
Have you tried using fibers/Future to handle async response?
Hi @gurjit03 ! I'm faced with same problem. Have you found a solution? Could you please share it? Thanks!
@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 I have solved it finally. Thank you for feedback.
@achirkof then kindly share it might be helpful for people coming to this thread.