req.body changes value after assignment to variable

es6: use destructuring assignment to assign variables from objects
express request body
pm.request.body postman
es6: use destructuring assignment to assign variables from nested objects
destructure to existing variable
req.query express
es6 destructuring into new object
use object destructuring

i don't really understand what's happening here and would appreciate pointing out flaws in my assumptions:

 putUser: async (req, res, next) => {
        console.log('test1', req.body)
        let data = req.body
        data["local.password"] = 'xd121244212141243'
        console.log('test2', data)
        console.log('test3', req.body)

this block of code produces result as:

test1 { 'local.username': 'name','local.password': 'passwordToChange' }
test2 { 'local.username': 'name','local.password': 'xd121244212141243' }
test3 { 'local.username': 'name','local.password': 'xd121244212141243' }

So the question is, why does value of req.body changes ? Is this because we are pointing to the object instead of making new one ?

Is this because we are pointing to the object instead of making new one ?


 let data = req.body

That copies the reference to the object from req.body to data, so they are both referencing the same object. To copy instead:

 let data = { ...req.body };

ES6: Use Destructuring Assignment to Assign Variables from , function getLength(str) { "use strict"; // change code below this line const length And sometimes, we would like to assign those values into variables: I'd recommend MDN web docs - Destructuring assignment after understanding the basics. get user registration input */ let username = req.body.username; let password  This variable takes its value from a command substitution. This is the command phrase between the parentheses $( ). Note there’s a dollar sign $ before the first parenthesis. This construct $( ) evaluates the commands within the parentheses, and then returns their final value. In this example, that value is assigned to the file_count variable.

Yup, see this S.O. post about it.

Tl;dr: "Javascript is always pass by value, but when a variable refers to an object (including arrays), the "value" is a reference to the object."

Change request body in pre-request script · Issue #4808 , For example, request body set to this: and pre-request script set to numaanashraf assigned a85 and vkaegis on Jul 11, 2018 Storing the value in the environment variable and manipulating that codenirvana moved this from Pending triage to Later in Runtime Triage and Development on Jun 18, 2019. If value is specified, then the contents of the variable varName are set equal to value. If varName consists only of alphanumeric characters, and no parentheses, it is a scalar variable. If varName has the form varName(index) , it is a member of an associative array.

You are correct both data and req.body point to the same location in memory.

You could fix this in many ways see this question for most of them.

One way which I think should suit your specific case is

let data = JSON.parse(JSON.stringify(req.body))

though it isn't particularily efficient.

API Reference, A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body ), or an empty object ( {} ) if there If this is a number, then the value specifies the number of bytes; if it is a string, the value is requests during the life of the maxAge option to check if the file has changed. Having no initial value for the @EmpName variable, and no value returned from the subquery, a NULL value will be assigned to that variable in both cases as shown clearly in the result message below: If the previously declared SQL variable has an initial value, and the subquery that is used to assign a value to the variable returns no value, the SET and SELECT statement will behave in different ways.

Isomorphic Go: Learn how to build modern isomorphic web , Print("Encountered error when attempting to read the request body: ", err) } err We create a new FormParams instance and assign it to the variable formParams. Doing so will change the default behavior of fetching the form value for a  This is kind of a 'common' mistake. Pipes create SubShells, so the while read is running on a different shell than your script, that makes your CNT variable never changes (only the one inside the pipe subshell). Group the last echo with the subshell while to fix it (there are many other way to fix it, this is one. Iain and Ignacio's answers

Node.js Tutorial for Beginners (Part 3): Transferring Data Between , After choosing EJS, we set about creating the basic HTML make-up of the application. create the following variable at the top of the index.js file: const bodyParser This object will contain key-value pairs, where the value can be a model and data model to update, and provide a way to track changes. Set Cell Value. To set a Cell Value, use the Value property of the Range or Cells object. Range.Value & Cells.Value. There are two ways to reference cell(s) in VBA: Range Object – Range(“A2”).Value; Cells Object – Cells(2,1).Value; The Range object allows you to reference a cell using the standard “A1” notation.

var, The scope of a variable declared with var is its current execution and the variable will not lose its value, unless another assignment is This means its property descriptor cannot be changed and it cannot be Consequently, after the first line, x === undefined && y === 'A' and send us a pull request. After a structure enhancement, the assignment or comparison may no longer be permitted. syntactically incorrect. Posted on May 23, 2016 at 05:02 AM | 1.9k Views

  • Is this because we are pointing to the object instead of making new one ? - Yes