REMINDER
SOLVED fileSaveUploaded issue (appdrag-cloudbackend node package)
-
Hi there
I recently tried using the fileSaveUploaded method to rename files that are sent to the server throught a form.
I managed a way to work it out throught it, but the problem is that it sounds like it works only for input events named "FILES", I didn't find a way to make it works with events of other names (it actually give a response as it can't find the parameter 0 of an undefined list)...
I feel like we should use multi_input file upload element to navigate on files inside the event, but I really need to know if there is a way to make it work on a single form with multiple file upload inputs , of in other words, can I use my custom events names for this function to work proprely ?
for curious people : error stack of the case the event name isn't "FILES" but should still be pointing a valid input on the page
{"status":"OK","execTime":1256,"billedTime":1300,"payload":{"errorType":"TypeError","errorMessage":"Cannot read property '0' of undefined","trace":["TypeError: Cannot read property '0' of undefined"," at Runtime.exports.handler (/var/task/main.js:49:49)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]},"logs":"2021-07-22T13:56:19.821Z\teb22f29d-aa34-41a4-af7f-33a2bbf46435\tERROR\tInvoke Error \t{\"errorType\":\"TypeError\",\"errorMessage\":\"Cannot read property '0' of undefined\",\"stack\":[\"TypeError: Cannot read property '0' of undefined\",\" at Runtime.exports.handler (/var/task/main.js:49:49)\",\" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)\"]}\n"} appdrag.js:1:1119
-
@hamza-hamzaoui that's correct, uploaded files will be available in an array in event.FILES
There is no way to change this but it's not an issue, your code inside your backend function just have to read that collection
-
@jbenguira Thanks for the answer
Well, does that mean that anything I upload from a single form will be appended to the event FILES ? So the name of the entry from the form could be whatever ?
-
@undefined that's correct
-
@jbenguira Thx for your assistance, I now understand how the event FILES works.
I'm actually facing a new problem and I still didn't find a solution...
I'm trying to extract the cdnPath result from the promise that is generated throught the use of the fileSaveUploaded function.I tried multiple methods :
var Myentry = await appdrag.fileSaveUploaded(event["FILES"][0]["path"], "XX-" + MyFormEntry + "_" + AnotherFormEntry + extension).then( async function(response) { answer = await response; console.log(answer); } );
(This is the last one i tried)
The problem is that console.log returns a promise, that when pushed to the database as a "link" is replaces by [promise object], using a return only takes the first { in account and don't paste the rest, using response.cdnPath returns an undefined value.
So, I'm trying to extract the cdnPath link that the function generates to post it proprely to the database, could somebody give me an advice of how to proprely grab it to POST it as a direct link ?
Thanks in advance.
-
(the actual console.log content)
{"status":"OK","execTime":1173,"billedTime":1200,"payload":"null","logs":"2021-07-27T09:12:59.362Z\t1d9a7104-bd32-4cd4-b39b-0c9948e8085c\tINFO\t{\"status\":\"OK\",\"directPath\":\"//someawsserver.com/dev.appdrag.com/test-****-*-******/CloudBackend/uploads/TheFirstFile.extension\",\"cdnPath\":\"//cf.appdrag.com/test-*****-*-******/CloudBackend/uploads/TheFirstFile.extension\\"}\n2021-07-27T09:12:59.577Z\t1d9a7104-bd32-4cd4-b39b-0c9948e8085c\tINFO\t{\"status\":\"OK\",\"directPath\":\"//someawsserver.com/dev.appdrag.com/test****-*-******/CloudBackend/uploads/TheSecondFile.extension\",\"cdnPath\":\"//cf.appdrag.com/test-****-*-******/CloudBackend/uploads/TheSecondFile.extension\"}\n2021-07-27T09:12:59.641Z\t1d9a7104-bd32-4cd4-b39b-0c9948e8085c\tINFO\t{ \"status\" : \"OK\", \"execTime\" : \"9\", \"billedTime\" : \"9\", \"affectedRows\" : \"1\"}\n"}
-
Up ?
-
@undefined in your code you have :
answer = await response;
this is incorrect, you can directly do:
console.log(response);
also no need to have async before the inline response
var Myentry = await appdrag.fileSaveUploaded(event["FILES"][0]["path"], "XX-" + MyFormEntry + "_" + AnotherFormEntry + extension).then( function(response) { console.log(response); } );
in fact you don't need to mess at all with async/await, just this should do it
appdrag.fileSaveUploaded(event["FILES"][0]["path"], "XX-" + MyFormEntry + "_" + AnotherFormEntry + extension).then( function(response) { console.log(response); } );
-
@joseph-benguira Hi ! Thanks for taking time to answer me
Well, the point is that I tried with the method you describe at first, then found out that it was sending a whole promise to the database, which can't be correctly used as an URL (direct link to the uploaded file), so I was trying to find a way to extract the cdnPath from that promise so I could get the link only, not the whole thing (The console print in my previous message shows what the console.log(response) returns to the browser).
The whole await & async things where just desperate attempts from me to post something else to the database than a whole promise object ...Well wait, i'll try again with your exact syntax replacing with my things and get back to here ...
-
Same problem, it stores the whole object instead of the specific url, didn't find a way to deal with it after few attempts, if somebody has another solution I would love to hear from it
-
@undefined I have quickly checked one project where I did this and here is the code:
if (event.FILES.length > 0) { file = JSON.parse(await appdrag.fileSaveUploaded( event["FILES"][0]["path"], `myfolder/${event["FILES"][0]["path"].split('/')[1]}`))['cdnPath']; }
so here the thing is you indeed need to await before calling appdrag.fileSaveUploaded, and also you need to parse the response as JSON, then you can access the field cdnPath
-
@joseph-benguira Works like a charm ! Thanks !
(should I close the topic or something ?)
-
@undefined Done, marked as solved