File uploading though scripts in pb_hooks folder #4956
-
I have been struggling with the codes below for a couple of days. I don't know what I am doing wrong. I referred to the links below to come up with the codes. It has no problem creating records for the 'transDesc' and 'transAmount' fields, but it does not upload files to the 'picture' field through JavaScripts in the pb_hooks folder.
It has no problem creating records for 'transDesc' and 'transAmount', but it does not upload files using pb_hooks. If I use the following code directly inside submitForm(), it uploads files: I am learning javascript and how to use Pocketbase. Can anyone give me advice? By they way, thank you so much for this great application!!! ` ` ============================= ` // manually upload file(s) // const f2 = $filesystem.fileFromPath("picture"); // multipart uploaded files try { ` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm having difficulties understanding the above code and what actually it is its purpose or its desired behavior ( Note that you don't need to manually call routerAdd("POST", "/accTrans", (c) => {
const collection = $app.dao().findCollectionByNameOrId("accTrans");
const record = new Record(collection);
const form = new RecordUpsertForm($app, record);
form.loadRequest(c.request(), "");
try {
form.submit();
} catch (err) {
throw new BadRequestError("Failed to create record", err);
}
return c.json(200, { success: true });
}); In any case, keep in mind that the |
Beta Was this translation helpful? Give feedback.
I'm having difficulties understanding the above code and what actually it is its purpose or its desired behavior (
c.request().formFile("")
doesn't make sense; also there is no need forasync
in the hook handler).Note that you don't need to manually call
form.loadData
andform.addFiles
if you useform.loadRequest
.form.loadRequest
takes care for loading all request fields (both regular data and files) into the form and its related record (as long as the submitted request field names match with the collection schema). Or in other words, your route could be simply: