2

I'd like to send list of objects through postman tool and receive it on my server side code, This is the json code i'm sending through postman tool and receiving it throught post method

{
    "user1" : {
        "name" : "grijan",
        "prof" : "student"
    },
    "user2" : {
        "name" : "vijay",
        "prof" : "teacher"
    }
}

My server side code

var express = require('express');
var app = express();


app.use(express.urlencoded({ extended: false }));
app.use(express.json());

app.post('/saveUser',function(req,res){
    var  obj= req.body;
    console.log(obj);
    //code for iteration ????
})

var server = app.listen(8008,function(){});

I need to iterate through the objects and the fields in the objects! FYI i'm new to node this is my 2nd day :)

Grijan
  • 287
  • 5
  • 22
  • Possible duplicate of [How to iterate over a JavaScript object?](https://stackoverflow.com/questions/14379274/how-to-iterate-over-a-javascript-object) – str Dec 05 '17 at 11:09

2 Answers2

2

You are not sending a list, but an object. If you want to send a list you should send something like this:

[
    {
        "name" : "grijan",
        "prof" : "student"
    },
    {
        "name" : "vijay",
        "prof" : "teacher"
    }
]

And them in the node side:

var express = require('express');
var app = express();

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

var methodToSaveInDB = function(name, prof) {
    // save it here into DB
}

app.post('/saveUser',function(req,res){
    var usersList = req.body;
    for(var user of usersList) {
        methodToSaveInDB(user.name, user.prof);
    }
})

var server = app.listen(8008,function(){});

If you need to send it like an object and not as a list, you could loop through object properties:

for (var property in obj) {
    if (obj.hasOwnProperty(property)) {
        console.log(obj[property].name);
        console.log(obj[property].prof);
    }
}
David Vicente
  • 3,091
  • 1
  • 17
  • 27
  • David i'm very poor in node, is this the code to iterate through objects for my program for (var property in obj) { if (obj.hasOwnProperty(property)) { // do stuff } } – Grijan Dec 05 '17 at 11:19
  • can you show me how to iterate in such a way which gives me all the four fields separately – Grijan Dec 05 '17 at 11:21
  • i'm trying to put all these fields in database, so need separate values – Grijan Dec 05 '17 at 11:23
  • @Grijan I eddited the response with both possibilities. – David Vicente Dec 05 '17 at 11:27
  • The things i ask might sound dumb but i need to pass these objects to another function and add those values into a db containing name and prof, any idea on how to do it?, sorry if i am disturbing you! – Grijan Dec 05 '17 at 12:07
  • @Grijan check new response version – David Vicente Dec 05 '17 at 12:27
1

Anyone who is looking for the latest and Built-in solution. We can use Object.entries() method which is introduced in ES7

const obj = { a: 5, b: 7, c: 9 };

for (const [key, value] of Object.entries(obj)) {
  
  //you can do your operations here
  console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"

}

you can find additional features and examples of Object.entries here enter link description here

Sharoon Ck
  • 738
  • 7
  • 14