'source.on is not a function' while uploading PDF

This issue is identical to this post , however the proposed solution there didn’t seem to have anything to do with the actual issue, or more likely, I didn’t understand the solution.
( @maxbjork , if you happen to have any thoughts, I’d welcome them!)

In essence, I am trying to upload a pdf via mediaManager, but when doing so receive a promise rejection with the error ‘source.on is not a function.’

Relevant code attached.


Front end, passes a pdf encoded in base64
const pdfDataUri = await pdfDoc . saveAsBase64 ({ dataUri : true });
thisContract = pdfDataUri ;
$w ( ‘#contractFrame’ ). postMessage ( thisContract );
await addPaperworkToStaff ( pdfDataUri , ‘contract’ , info.lastName , info._id );


backend, {SHOULD} upload file, retrieve the fileURL, then attach that to a database field.

export async function uploadFile ( file , type , name ) {

//FAILURE OCCURS HERE.
let fileResults = await mediaManager . upload (
/paperwork/ ${ type } ,
Buffer . from ( file , ‘base64’ ),
${ name } _ ${ type } .pdf ,
{
“mediaOptions” : {
“mimeType” : “application/pdf” ,
“mediaType” : “document”
},
“metadataOptions” : {
“isPrivate” : false ,
“isVisitorUpload” : false ,
}
});
return fileResults ;

}

export async function addPaperworkToStaff ( file , type , name , staff_id ){
console . log ( “Starting file upload.” );
let uploadedFile = await uploadFile ( file , type , name );

console . log ( "Getting staff object from id." ); 
let  info  =  **await**  wixData . **get** ( "staff" ,  staff_id ); 

console . log ( "Adding paperwork to retrieved staff object" ); 
info [ "onboardingPaperwork" ] +=  uploadedFile.fileUrl ; 

console . log ( "Updating database with new staff member." ); 
let  results  =  **await**  wixData . update ( "staff" ,  info ); 

console . log ( "returning results" ); 
**return**  results ; 

}

@yisrael-wix and @code-queen , I know you’ve both helped with similar issues. Any thoughts?

UPDATE: This has been (partially) solved. There is a built in Buffer module that was being overridden by an imported Buffer module. Removing that import allowed the code to work as expected, however, the pdfs that are being uploaded now are blank gray boxes.

Hey, is there a reason you are saving as Base64? I’m no expert, but to get it to work, I had to do this (raw buffer write then read):

// Notice below that I just use pdfDoc.save() – and write it locally
fs . writeFileSync ( __dirname + “/” + fileName , await pdfDoc . save ()); // WRITE
let buffer = fs . readFileSync ( __dirname + “/” + fileName ); // READ

const newFileInfo = await mediaManager . upload (
“/thisReallyCoolFolder” ,
buffer ,
fileName ,
{
“mediaOptions” : {
“mimeType” : “application/pdf” ,
“mediaType” : “document”
},
“metadataOptions” : {
“isPrivate” : false ,
“isVisitorUpload” : false
}
});

@maxbjork that’s a great question and is unequivocally why I’m having issues post-save. I’m encoding to base64 to pass it to an iframe for display, but I’m realizing it’s probably a better idea to encode in another way that the iframe can still display…

So I’ve made some adjustments, but when I attempt to use the code you listed I get an error:

“[“The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object”]”


This occurs when I attempt to use writeFileSync. The only difference that I can think of is that I’m passing the result of await pdfDoc.save() from the front end to the backend, but it’s not like it’s magically changing types while being passed.

Hmmmm not sure – I wish I could be more help, but honestly, there’s a huge knowledge gap for me here. As soon as I find a solution, I don’t tend to dig into it too much.
I think it’s reasonable to at least try doing everything on the backend to see if it works and then (if necessary) use a frontend to backend implementation.

Just see if you can create a new pdf with pdf-lib then save it with media manager. Make sure that works (the Test Function option works with media manager). Then either A) Try using the front end again, or B) Ask yourself if it’s even necessary to do the work partly on the client-side-- why not all backend?

@maxbjork That’s actually exactly what I was thinking about while lying in bed. I finally realized why your solution worked the way it did and I think may be the route that I go if I can get it to work correctly. Thank you for replying at all and the help you’ve provided. Will update and close once code is working!