I want to save the images into the database uploaded from the React.js but it is not getting uploaded. It works fine in the postman but not in the React.js.
In the database I am saving only the file's path not the full data [Array] of the file.
The state: let [preview_image, setPreview_Image] = useState("")
The file handler function:
const uploadFileHandler = async (e) => {
e.preventDefault();
const file = e.target.files[0]; //The file logs the data about the file
let formData = new FormData();
let image = formData.append("preview_image", file); //Here image logs the undefined in the console.
// So, basically it is not appending the file-data to the formData
setUploading(true);
try {
const config = {
url: "http://localhost:8000/v1/template/create",
method: "POST",
data: formData,
headers: {
"Content-Type": "multipart/form-data",
"x-access-token": adminInfo.data.JWToken,
},
};
const { data } = await axios(config).catch(console.error);
setPreview_Image(data);
setUploading(false);
} catch (error) {
console.trace(error);
setUploading(false);
}
};
The file uploader:
<Form.Group controlId="preview_image">
<Form.Label>Image</Form.Label>
<Form.Control
type="text"
placeholder="Upload Image url"
value={preview_image}
onChange={(e) => setPreview_Image(e.target.value)}
></Form.Control>
<Form.Control
type="file"
label="Choose file"
onChange={uploadFileHandler}
></Form.Control>
</Form.Group>
The template-creator action:
export const createtemplate = (templateData) => async (dispatch, getState) => {
try {
dispatch({ type: TEMPLATE_CREATE_REQUEST });
const {
adminLogin: { adminInfo },
} = getState();
const config = {
url: "http://localhost:8000/v1/template/create",
method: "POST",
data: templateData,
headers: {
"Content-Type": "application/json",
"x-access-token": adminInfo.data.JWToken,
},
};
const { data } = await axios(config).catch(console.error);
dispatch({ type: TEMPLATE_CREATE_SUCCESS, payload: data });
} catch (error) {
dispatch({
type: TEMPLATE_CREATE_FAILURE,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
The request and response from the postman
Not sure why I am getting the error.
The templateScreen where I am calling the createtemplate action:
const submitHandler = (e) => {
e.preventDefault();
dispatch(
createTemplate({
name: templateName,
package_ids: selectedPackage,
html_template: htmlTemplate,
required_variables: variables,
preview_image: preview_image,
})
);
};
BACKEND CONFIGURATION
The template creator service:
exports.createTemplate = async (req) => {
// HERE req.files logs undefined after I click on the add template details button and req.body logs all the data.
const name = req.body.name;
const package_ids = req.body.package_ids;
const html_template = req.body.html_template;
const required_variables = req.body.required_variables;
const preview_image = req.files.preview_image;
const imagePath = preview_image.map((image) => image.path);
const template = new Template({
name,
package_ids,
html_template,
required_variables,
preview_image: imagePath.toString(),
});
await template.save();
return template;
};
The controller file where the createTemplate service
is being called
exports.createTemplate = catchAsync(async (req, res) => {
try {
const template = await templateService.createTemplate(req);
return res.succeed(template, "Template created successfully");
} catch (error) {
console.trace(error.toJSON());
return res.failed(500, "Internal Server Error", error);
}
});
What it basically does is when I click on choose file and clicks on the files to upload it dispatches the create template action (that's what I think). Here it logs the files details and also responds with the following error from the backend's controller file: