Uploading files with Multer and handling the JSON object

Is there a way to send both a file and a JSON object containing data using multer? I came across this thread, but it only explains how to attach one field at a time.

Here's what I currently have on the client side:

request
  .post(uploadPOSTUrl)
  .set('Accept', 'application/json')
  .field('Test', object.TestField)
  .attach('file', file)
  .end((err, res) => {
    if (err) {

    } else {

    }
  });

and on the server side

 export function upload(req, res){
    console.log("UploadedJSON: ", req.body);
    console.log("UploadedFile: ",req.file); 
    res.status(204).end();
}

However, instead of just sending one field, I need to send the entire object .field('Test', object). When I try this, I receive [Object object] on the server side and can't access the fields.

At the moment, my only solution would be to loop through and add .field() for every field in my object...

Answer №1

It appears that your front-end code is utilizing the SuperAgent library. In order to send multipart requests using SuperAgent instead of multer, the key is in understanding how to do so effectively.

You can refer to the SuperAgent documentation on multipart requests for guidance. The process involves using the .field() method multiple times as shown below:

 request
   .post('/upload')
   .field('user[name]', 'Tobi')
   .field('user[email]', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="473328252e072b2226352925282834336924282a">[email protected]</a>')
   .attach('image', 'path/to/tobi.png')
   .end(callback);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adaptive Container with Images that are not stretched to full width

Is there a way to achieve the same effect as seen in images 2 and 3 here: Although these images already have their own "padding," I'm curious if it can be replicated using just jQuery and CSS? I would appreciate any help or insights on this. Thank y ...

How jQuery stops the submission of a form

Sample HTML Code <p><select name="status" class="form-control" id="showThisItem"> <option value=""> Select Status </option> <option value="Paid"> Paid </option> <option value="Unpa ...

Implement rounded corners on D3js Donut Chart

In my AngularJS application, I have successfully implemented a donut chart as shown below: https://i.stack.imgur.com/3GVwN.png However, the design mockup indicates that we would like to achieve this look, with a border-radius property applied to the gree ...

Having trouble accessing HikVision Web SDK functions in Angular 17

First and foremost, this concerns the HikVision cameras specifically. Because the SDK is compiled to plain JavaScript (with jQuery included), I had to import the JS files in angular.json and use declare in the component TS file (in this instance, camera.c ...

JavaScript Method for Retrieving Form Element Values

I am currently working on retrieving the value of an input element in a popup window that is launched from a parent page. Here is my code, but I am facing difficulties in obtaining the pc value from the input field. <html> <head> <? $pid= ...

Experiencing challenges with showcasing numerical values in the grid cells

My latest project involves creating an interactive sudoku solver. I've successfully created a grid made up of various elements to resemble an empty sudoku grid. However, my challenge lies in getting the numbers to display within the grid cells. Click ...

SecurityError: The dubious operation triggers CORS to persist in its insecurities

I have developed an HTTP server using Express in Node.js. This server is currently running locally on port 3000. There is a served HTML page called "index.html" which makes AJAX GET requests for contents (in HTML format). These AJAX contents are also serv ...

I'm seeking assistance in enhancing this code within tb for extracting values using JavaScript

Currently, I am extracting values from a table using JavaScript. The code seems to be functioning correctly, but I am looking for ways to optimize it and ensure that it is compatible with most modern browsers. The list in the table undergoes frequent chang ...

What is the significance of using composability over the deprecated method in Reactjs Material-UI menuItems?

I have taken over a project that was built using an older version of react, and I am currently in the process of updating it. However, I encountered console errors right off the bat. Error : bundle.js:6263 Warning: The "menuItems" property of the "Left ...

Can React components be saved in an array?

I am currently working on enhancing the code snippet provided below. This code is intended to iterate through elements of an array using keys to locate images within a lengthy SVG file directly embedded in the document under the identifier "SomelongUglySVG ...

What is the best way to expand a jade layout using a view/parent/child hierarchy?

My Views are organized in a specific structure. I have been trying to extend the layout.jade to all jade files located under my user folder. However, using extends ../layout in the files under the user folder doesn't seem to work as expected. Unfortun ...

You have encountered an issue with the runtime-only build of Vue, which does not include the template compiler

Lately, I have been utilizing Vue in a project and encountered an issue where upon compiling, my browser page displays as white with an error message stating "You are using the runtime-only build of Vue where the template compiler is not available. Either ...

Tips for swapping a component with another component in React.js without the need for a page refresh

class Navigation extends Component { constructor(props) { super(props); this.state = { width: window.innerWidth, } } updateWidth = () => { if (this.state.width > 700) { this.setStat ...

Utilize JavaScript's $.post function to export PHP $_POST data directly into a file

After spending hours trying to figure this out, I've come to the realization that I am a complete beginner with little to no knowledge of what I'm doing... The issue I'm facing is related to some JavaScript code being triggered by a button ...

What is the proper way to confirm the authenticity of a captcha code

hey there, I'm struggling with my captcha code. The issue is that it still accepts wrong captchas when entered in the input box. Can someone guide me on how to validate the wrong captcha entry? Here's my form code: <p class="Captcha" style=" ...

Tips for loading images dynamically (or lazily) as they come into the user's view with scrolling

Many modern websites, such as Facebook and Google Image Search, display images below the fold only when a user scrolls down the page enough to bring them into view (even though the page source code shows X number of <img> tags, they are not initially ...

Having Difficulty Configuring Async Request Outcome

I'm struggling to access the outcome of an asynchronous request made to an rss feed. After reading a post on How do I return the response from an asynchronous call?, which recommended using Promises, I tried implementing one. However, even though the ...

Passport.js encountered an issue when attempting to serialize the user for the session

I'm having trouble with the Passport.js module and Express.js. My code is set up for a hardcoded login for testing purposes. However, I keep getting the error message: I've searched online for solutions on Stack Overflow, but I can't seem ...

ways to run php script based on screen resolution or size

I have 2 php files that I need to execute based on screen size. include_once('large.php'); include_once('small.php'); The condition is to run large.php when the screen size is greater than 992px, and small.php when the screen size is ...

Revert animation back to its initial position with the help of JavaScript

After implementing the script below, I successfully managed to shift my image to the right upon clicking: <script> var myTimer = null; function move(){ document.getElementById("fish").style.left = parseInt(document.getElementById("fish ...