how to save multiple for loops data?How to send headers after loopHow to query MongoDB with “like”?How to retrieve POST query parameters?ExpressJS How to structure an application?How to get GET (query string) variables in Express.js on Node.js?How to allow CORS?Mongod complains that there is no /data/db folderHow do I drop a MongoDB database from the command line?How to list all collections in the mongo shell?“Large data” work flows using pandasInsert geojson polygons into mongodb and query, based on the coordinates of a point, all the polygons which are located at a certain distance from it

Is there metaphorical meaning of "aus der Haft entlassen"?

A strange hotel

Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?

Von Neumann Extractor - Which bit is retained?

How much of a wave function must reside inside event horizon for it to be consumed by the black hole?

A Paper Record is What I Hamper

Is Diceware more secure than a long passphrase?

What is the unit of time_lock_delta in LND?

Why do distances seem to matter in the Foundation world?

Why do games have consumables?

Drawing a german abacus as in the books of Adam Ries

How can I practically buy stocks?

Who's the random kid standing in the gathering at the end?

Cayley's Matrix Notation

What was Apollo 13's "Little Jolt" after MECO?

SFDX - Create Objects with Custom Properties

Contradiction proof for inequality of P and NP?

Will I lose my paid in full property

How important is it that $TERM is correct?

What *exactly* is electrical current, voltage, and resistance?

How to have a sharp product image?

A faster way to compute the largest prime factor

How do I reattach a shelf to the wall when it ripped out of the wall?

"The cow" OR "a cow" OR "cows" in this context



how to save multiple for loops data?


How to send headers after loopHow to query MongoDB with “like”?How to retrieve POST query parameters?ExpressJS How to structure an application?How to get GET (query string) variables in Express.js on Node.js?How to allow CORS?Mongod complains that there is no /data/db folderHow do I drop a MongoDB database from the command line?How to list all collections in the mongo shell?“Large data” work flows using pandasInsert geojson polygons into mongodb and query, based on the coordinates of a point, all the polygons which are located at a certain distance from it






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















In Post request, I want to pull objectId's data and save it in the database. There will be 2 or 3 objectId's which will I will be getting it in array. From that array I am able to pull the data with separate "for loops" but at the end I am not able to save the data.
I want to know how to move data from one array to another or directly save the data.



Schema:--



const jobSch = new Schema(
token : type:Number,
name : type:String,
phoneNumber: type:Number,
gender : type:String,
stage : type:String,
categoryId : [Schema.Types.ObjectId],
subCategoryId : [Schema.Types.ObjectId],
serviceId : [Schema.Types.ObjectId],
category : [String],
subcategory : [String],
service: [String],
paymenttype: [String],
tax: type:Number,
totalprice: type:String,
servicePerson: type:String,
createdOn : type:Date, default:new Date(),
updatedOn : type:Date, default:new Date()
);


POST request code:--



router.post('/', async (req, res) => 
let ids = req.body;
let catId = ids.categoryId;
console.log("catId", catId)
console.log("catId.length", catId.length)
let subcatId = ids.subCategoryId;
console.log("subcatId", subcatId);
console.log("subcatId.length", subcatId.length)
let serId = ids.serviceId;
console.log("serId", serId);
console.log("subcatId.length", serId.length)
let job = new Job(ids);
let catgy=[];
let subcatgy=[];
let subser=[];

for (i = 0; i < catId.length; i++)
Category.findById(catId[i])
.then(category =>
if (category && category != null)
console.log('category.name', category.name);
catgy.push(`$category.name`)
console.log(catgy);


)

for (i = 0; i < subcatId.length; i++)
Service.findById(subcatId[i])
.then(service =>
if (service && service != null)
console.log('service.name', service.name);
subcatgy.push(`$service.name`)
console.log(subcatgy);


)

for (i = 0; i < serId.length; i++)
Subservice.findById(serId[i])
.then(subService =>
if (subService && subService != null)
console.log('subservice.name', subService.subService);
subser.push(`$subService.subService`)
console.log(subser);

)

var category = catgy.slice();
console.log("category1", category)
var service = subcatgy.slice();
console.log('service2', service);
var subService = subser.slice();
console.log('subService3',subService);
console.log(job);
job.save().then(job =>
res.status(201).json(
message: 'Job saved successfully', job
)
).catch(err => console.log(err));
);









share|improve this question






















  • i got a different solution for pushing the data.. But thanks the link which u had give gave me a rough idea!!! @RenatoVassao

    – user10009323
    Mar 11 at 10:55


















0















In Post request, I want to pull objectId's data and save it in the database. There will be 2 or 3 objectId's which will I will be getting it in array. From that array I am able to pull the data with separate "for loops" but at the end I am not able to save the data.
I want to know how to move data from one array to another or directly save the data.



Schema:--



const jobSch = new Schema(
token : type:Number,
name : type:String,
phoneNumber: type:Number,
gender : type:String,
stage : type:String,
categoryId : [Schema.Types.ObjectId],
subCategoryId : [Schema.Types.ObjectId],
serviceId : [Schema.Types.ObjectId],
category : [String],
subcategory : [String],
service: [String],
paymenttype: [String],
tax: type:Number,
totalprice: type:String,
servicePerson: type:String,
createdOn : type:Date, default:new Date(),
updatedOn : type:Date, default:new Date()
);


POST request code:--



router.post('/', async (req, res) => 
let ids = req.body;
let catId = ids.categoryId;
console.log("catId", catId)
console.log("catId.length", catId.length)
let subcatId = ids.subCategoryId;
console.log("subcatId", subcatId);
console.log("subcatId.length", subcatId.length)
let serId = ids.serviceId;
console.log("serId", serId);
console.log("subcatId.length", serId.length)
let job = new Job(ids);
let catgy=[];
let subcatgy=[];
let subser=[];

for (i = 0; i < catId.length; i++)
Category.findById(catId[i])
.then(category =>
if (category && category != null)
console.log('category.name', category.name);
catgy.push(`$category.name`)
console.log(catgy);


)

for (i = 0; i < subcatId.length; i++)
Service.findById(subcatId[i])
.then(service =>
if (service && service != null)
console.log('service.name', service.name);
subcatgy.push(`$service.name`)
console.log(subcatgy);


)

for (i = 0; i < serId.length; i++)
Subservice.findById(serId[i])
.then(subService =>
if (subService && subService != null)
console.log('subservice.name', subService.subService);
subser.push(`$subService.subService`)
console.log(subser);

)

var category = catgy.slice();
console.log("category1", category)
var service = subcatgy.slice();
console.log('service2', service);
var subService = subser.slice();
console.log('subService3',subService);
console.log(job);
job.save().then(job =>
res.status(201).json(
message: 'Job saved successfully', job
)
).catch(err => console.log(err));
);









share|improve this question






















  • i got a different solution for pushing the data.. But thanks the link which u had give gave me a rough idea!!! @RenatoVassao

    – user10009323
    Mar 11 at 10:55














0












0








0








In Post request, I want to pull objectId's data and save it in the database. There will be 2 or 3 objectId's which will I will be getting it in array. From that array I am able to pull the data with separate "for loops" but at the end I am not able to save the data.
I want to know how to move data from one array to another or directly save the data.



Schema:--



const jobSch = new Schema(
token : type:Number,
name : type:String,
phoneNumber: type:Number,
gender : type:String,
stage : type:String,
categoryId : [Schema.Types.ObjectId],
subCategoryId : [Schema.Types.ObjectId],
serviceId : [Schema.Types.ObjectId],
category : [String],
subcategory : [String],
service: [String],
paymenttype: [String],
tax: type:Number,
totalprice: type:String,
servicePerson: type:String,
createdOn : type:Date, default:new Date(),
updatedOn : type:Date, default:new Date()
);


POST request code:--



router.post('/', async (req, res) => 
let ids = req.body;
let catId = ids.categoryId;
console.log("catId", catId)
console.log("catId.length", catId.length)
let subcatId = ids.subCategoryId;
console.log("subcatId", subcatId);
console.log("subcatId.length", subcatId.length)
let serId = ids.serviceId;
console.log("serId", serId);
console.log("subcatId.length", serId.length)
let job = new Job(ids);
let catgy=[];
let subcatgy=[];
let subser=[];

for (i = 0; i < catId.length; i++)
Category.findById(catId[i])
.then(category =>
if (category && category != null)
console.log('category.name', category.name);
catgy.push(`$category.name`)
console.log(catgy);


)

for (i = 0; i < subcatId.length; i++)
Service.findById(subcatId[i])
.then(service =>
if (service && service != null)
console.log('service.name', service.name);
subcatgy.push(`$service.name`)
console.log(subcatgy);


)

for (i = 0; i < serId.length; i++)
Subservice.findById(serId[i])
.then(subService =>
if (subService && subService != null)
console.log('subservice.name', subService.subService);
subser.push(`$subService.subService`)
console.log(subser);

)

var category = catgy.slice();
console.log("category1", category)
var service = subcatgy.slice();
console.log('service2', service);
var subService = subser.slice();
console.log('subService3',subService);
console.log(job);
job.save().then(job =>
res.status(201).json(
message: 'Job saved successfully', job
)
).catch(err => console.log(err));
);









share|improve this question














In Post request, I want to pull objectId's data and save it in the database. There will be 2 or 3 objectId's which will I will be getting it in array. From that array I am able to pull the data with separate "for loops" but at the end I am not able to save the data.
I want to know how to move data from one array to another or directly save the data.



Schema:--



const jobSch = new Schema(
token : type:Number,
name : type:String,
phoneNumber: type:Number,
gender : type:String,
stage : type:String,
categoryId : [Schema.Types.ObjectId],
subCategoryId : [Schema.Types.ObjectId],
serviceId : [Schema.Types.ObjectId],
category : [String],
subcategory : [String],
service: [String],
paymenttype: [String],
tax: type:Number,
totalprice: type:String,
servicePerson: type:String,
createdOn : type:Date, default:new Date(),
updatedOn : type:Date, default:new Date()
);


POST request code:--



router.post('/', async (req, res) => 
let ids = req.body;
let catId = ids.categoryId;
console.log("catId", catId)
console.log("catId.length", catId.length)
let subcatId = ids.subCategoryId;
console.log("subcatId", subcatId);
console.log("subcatId.length", subcatId.length)
let serId = ids.serviceId;
console.log("serId", serId);
console.log("subcatId.length", serId.length)
let job = new Job(ids);
let catgy=[];
let subcatgy=[];
let subser=[];

for (i = 0; i < catId.length; i++)
Category.findById(catId[i])
.then(category =>
if (category && category != null)
console.log('category.name', category.name);
catgy.push(`$category.name`)
console.log(catgy);


)

for (i = 0; i < subcatId.length; i++)
Service.findById(subcatId[i])
.then(service =>
if (service && service != null)
console.log('service.name', service.name);
subcatgy.push(`$service.name`)
console.log(subcatgy);


)

for (i = 0; i < serId.length; i++)
Subservice.findById(serId[i])
.then(subService =>
if (subService && subService != null)
console.log('subservice.name', subService.subService);
subser.push(`$subService.subService`)
console.log(subser);

)

var category = catgy.slice();
console.log("category1", category)
var service = subcatgy.slice();
console.log('service2', service);
var subService = subser.slice();
console.log('subService3',subService);
console.log(job);
job.save().then(job =>
res.status(201).json(
message: 'Job saved successfully', job
)
).catch(err => console.log(err));
);






mongodb api express mongoose-schema






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 7:40









user10009323user10009323

12




12












  • i got a different solution for pushing the data.. But thanks the link which u had give gave me a rough idea!!! @RenatoVassao

    – user10009323
    Mar 11 at 10:55


















  • i got a different solution for pushing the data.. But thanks the link which u had give gave me a rough idea!!! @RenatoVassao

    – user10009323
    Mar 11 at 10:55

















i got a different solution for pushing the data.. But thanks the link which u had give gave me a rough idea!!! @RenatoVassao

– user10009323
Mar 11 at 10:55






i got a different solution for pushing the data.. But thanks the link which u had give gave me a rough idea!!! @RenatoVassao

– user10009323
Mar 11 at 10:55













1 Answer
1






active

oldest

votes


















1














The problem is that the callback function of the resolved promises (findById) are executed asynchronously. Try checking this out, I think it will help you.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075129%2fhow-to-save-multiple-for-loops-data%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The problem is that the callback function of the resolved promises (findById) are executed asynchronously. Try checking this out, I think it will help you.






    share|improve this answer



























      1














      The problem is that the callback function of the resolved promises (findById) are executed asynchronously. Try checking this out, I think it will help you.






      share|improve this answer

























        1












        1








        1







        The problem is that the callback function of the resolved promises (findById) are executed asynchronously. Try checking this out, I think it will help you.






        share|improve this answer













        The problem is that the callback function of the resolved promises (findById) are executed asynchronously. Try checking this out, I think it will help you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 14:11









        Renato VassãoRenato Vassão

        1015




        1015





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075129%2fhow-to-save-multiple-for-loops-data%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved