Explaining the Contrast: `req.body` vs `req.params.all()` in Sails.js

Can you explain the distinction between using req.body and req.params.all() within a sails controller?

Answer №1

In simple words, req.body consists of data extracted from the request body, such as the payload in a POST method. On the other hand, req.params.all() provides you with a set of parameters derived from various sources, ranked by priority:

  • The route (for instance, the id in /post/:id).
  • The request body itself
  • The query string associated with the request

Answer №2

One issue that caught my attention when utilizing req.params in a PUT request is:

PUT /endpoint/1
{
id: 5,
parameter: 8
}
the value of req.body.id replaces the one from req.params.all().id, resulting in req.params.all().id being set to 5.

This problem appears to have been resolved in Sails v1.

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

What is the best way to retrieve my data/json from req.body in Express?

Recently, I started working with node.js and encountered an issue while trying to access JSON data on my node.js server through a post request. The goal is to send this data to an API and then pass it back to my front-end JavaScript file. Despite being abl ...

Sending an array of files to an express backend: A step-by-step guide

I am having an issue sending an array of images to my backend for upload using multer-s3. The process involves sending the images from a React frontend to a Node Express backend. Once submitted from the frontend, the images are formatted as follows: image ...

Session-based Authorization

I'm completely new to working with express.js, and I've been facing an issue while trying to create a session-cookie after logging in. Even though I can initiate the session and successfully log in, the session data doesn't carry over to the ...

What is the best way to manage Page Refresh using Angular.js?

I recently followed the tutorial at http://scotch.io/bar-talk/setting-up-a-mean-stack-single-page-application This guide went over controllers and services using angular.js for a single-page application. However, when I try to directly access /pageName o ...

Saving numerous files with Promises

There is a Node URL (created using Express) that enables users to download static images of addresses. The calling application sends a request to the /download URL with multiple addresses in JSON format. The download service then calls Google Maps to save ...

CSS file not loading in an ExpressJS application

I'm facing an issue with my ExpressJS web app where only the HTML files are loading but the CSS rules are not being applied. I have linked the HTML file with the CSS file and also included express.static(path.join(__dirname, 'css')) in my ap ...

Is it necessary to utilize ngrok to expose both front and back ends in a MERN stack in order for CRUD operations to function properly?

Currently, I am working on developing a MERN app on my local machine. The front end portion of the application can be accessed at localhost:3000 While the back end is running on localhost:3003 In the front end code, there is a request configured as: ax ...

What is the best way to combine two AngularJS apps on a single Express server?

I have scoured the internet to find solutions for my specific problem, but none seem to work quite right. I am attempting to serve two separate AngularJS apps based on incoming subdomains using express-subdomain with express. While the code seems to serve ...

Unable to establish an external connection with the node

Currently, I am in the process of establishing a connection to a local server and running an app on port 8080 using node. Both node and apache are installed on my system. When Apache is active, I can access the server externally. However, when Node is runn ...

What is the process for sending a multipart/form-data form with an uploaded file to a separate server using Node.js (specifically Express.js)?

When sending a form with file using enctype="multipart/form-data" to node.js (using the express.js framework), how can I easily resend this post request to a different server without changing its contents? ...

Inform the user that an error has occurred when attempting to perform an invalid

While using redux promise middleware for my front end, I am wondering about the correct status code to throw from my backend in case of an error. I know that I can use res.status(500).json(something), but is 500 the appropriate code for all types of erro ...

Ways to send JWT in the request header to an Angular6 application

Managing users and sessions through an external application such as a Web Application Firewall (WAF) The external app sends a JWT with user information in the request header to the Angular6 app The Angular6 app needs to extract the information from the req ...

The absence of req.body in the app reflects an undefined state

I'm encountering an issue with my app and I believe showing you my code is the best way to explain the problem: var Meetup = require('./models/meetup'); module.exports.create = function (req, res) { var meetup = new Meetup(req.body); c ...

The conversion to ObjectId was unsuccessful for the user ID

I'm looking to develop a feature where every time a user creates a new thread post, it will be linked to the User model by adding the newly created thread's ID to the threads array of the user. However, I'm running into an issue when trying ...

What is the method for retrieving the POST data from a nuxtjs server middleware?

I'm having trouble accessing the POST data from a Nuxt.js server middleware. I have been successful with GET requests, but for POST, the body seems to be missing. When I try to access req.body, it returns undefined. ...

Livereload in Gulp fails to automatically restart the server

How can I make my gulp+livereload server automatically restart and update the page when JS files are changed? Below is a snippet from my gulpfile.js: var gulp = require('gulp'), livereload = require('gulp-livereload'), ...

"Utilizing passport authentication, JWT tokens, and res.render in

I am new to using passport-jwt and I have successfully configured the strategy. Everything is working fine as I am able to send the token to the client and extract it from the header using the method fromAuthHeaderAsBearerToken(). However, I encountered an ...

Dividing socket.io namespaces into distinct files

Having developed two socket.io applications, one for an interactive game and the other for a chat feature, I decided to segregate them by creating separate namespaces. Now, my objective is to extract these apps from the main file app.js and store them in ...

Querying user locations using Mongoose's geospatial capabilities

Currently delving into the world of nodeJS, utilizing express along with mongoDB and mongoose for ORM functionalities. The task at hand involves creating a User entity and persisting it in the database, while also retrieving the user's location data a ...

Challenges in MongoDB Aggregation: Overcoming obstacles in combining two collections

I am managing a collection of products and a collection of brands, where each product is associated with a brand. I aim to retrieve each product along with its corresponding brand information. Products Collection: { "_id" : ObjectId("64 ...