1

Currently, I'm developing an API with sails js + mongoDB for an mobile app. I want to save multiple data that send from mobile app and I catch it via req parameters (as object) in my controller's function.

Some parts of my controller code look like this :

var contactData = req.allParams().Contact;
looping.. {
    Contact.create(contactData[index]).exec(function addContact(err,createdContact){  });
}

Could you tell me how the best way to implement looping for save that multiple contact data to the database?

Regards, argi danu.

2
  • 1
    Are you seriously thinking of sending the data through request parameters. I would suggesting sending it as POST data. Commented Apr 8, 2016 at 11:24
  • Yes, i'm serious.. :D req.allParams() is returns the value of all parameters sent in the request, include the request body. My Contact Data is sent through the request body (POST) from the mobile app, not from the url path or query string (GET). My question is, how the best way to implement looping process (asynchronous) in that Contact data (Object), for save that into database? Commented Apr 11, 2016 at 6:04

1 Answer 1

1

Okay, So if you are aware of the 'name' attributes for the request parameters, then you can simply use the create method as -

var contactData = req.allParams();
var name = contactData.name // attribute name
var number = contactData.number // attribute name 

And after that simply call the create method

Contact.create({name : name, number :number}).exec(functionaddContact(err,createdContact){ 

    // DO Something
});

But if you strictly want to use for loops to get all the data from request parameters, then Consider the following scenario -

Convert the combination of req parameters into a JSON object by appending the contactData into [ ] as

   var json = '[' + contactData +']';

and then using the following code get the parameter values from the created JSON object as follows

 for (var property in obj) {

   if (obj.hasOwnProperty(property)) {

       if(typeof obj[property] === 'string') {

         console.log(property);  // provides the property name
         console.log(obj[property]); // provides the value of the property
     }
  }

May be this helps. It worked with SQL databases

Sign up to request clarification or add additional context in comments.

3 Comments

So I have to use a for loop and call the create function, in the loop process? my Contact Data in req params, should look like this : { "Contact" : [{ "fullname" : "fullname_for_first_data", "phone" : "phone_for_first_data" }, { "fullname" : "fullname_for_second_data", "phone" : "phone_for_second_data" }, ... and so on until the last data (multiple data) ] } My question is, how the best way to implement looping process (asynchronous) in that Contact Object, for save that into database?
Okay, then [Async] (github.com/caolan/async) would be the thing for you. I consider that you are getting the req data in form - var contact = [{ "fullname" : "fullname_for_first_data", "phone" : "phone_for_first_data" }, { "fullname" : "fullname_for_second_data", "phone" : "phone_for_second_data" }] And then use the forEachOf() method of the async module async.forEachOf(obj, function (value, key, callback) { // call the insert method in here for taking the value parameter callback(); });
Okay. You got my point, parth. Thanks for your answer. Now, I use async.map() for find and save that contact data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.