NodeJS async functions with mongoose [duplicate]









up vote
0
down vote

favorite













This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



How do I make the .create() function to wait for table to be filled before returning it.
Because data returns undefined



const Construct = require('../models/constructModel')
const TemplateConstruct = require('../models/constructTemplateModel')

exports.create = async function ()
TemplateConstruct.find().then(function (constructs)
let table =
constructs.forEach((construct) =>
let newconstruct = new Construct()
newconstruct.number = construct.number
newconstruct.name = construct.name
newconstruct.basePrice = construct.basePrice
newconstruct.baseMicrowave = construct.baseMicrowave
newconstruct.atomGain = construct.atomGain
newconstruct.save().then(table.push(newconstruct))
)
console.log(table)
return table
)
// return [ 'test' ]



working around this :



constructFactory.create().then(function (data) 
console.log(data)
)









share|improve this question















marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 18:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • return the promise.
    – Jonas Wilms
    Nov 11 at 18:09














up vote
0
down vote

favorite













This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



How do I make the .create() function to wait for table to be filled before returning it.
Because data returns undefined



const Construct = require('../models/constructModel')
const TemplateConstruct = require('../models/constructTemplateModel')

exports.create = async function ()
TemplateConstruct.find().then(function (constructs)
let table =
constructs.forEach((construct) =>
let newconstruct = new Construct()
newconstruct.number = construct.number
newconstruct.name = construct.name
newconstruct.basePrice = construct.basePrice
newconstruct.baseMicrowave = construct.baseMicrowave
newconstruct.atomGain = construct.atomGain
newconstruct.save().then(table.push(newconstruct))
)
console.log(table)
return table
)
// return [ 'test' ]



working around this :



constructFactory.create().then(function (data) 
console.log(data)
)









share|improve this question















marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 18:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • return the promise.
    – Jonas Wilms
    Nov 11 at 18:09












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



How do I make the .create() function to wait for table to be filled before returning it.
Because data returns undefined



const Construct = require('../models/constructModel')
const TemplateConstruct = require('../models/constructTemplateModel')

exports.create = async function ()
TemplateConstruct.find().then(function (constructs)
let table =
constructs.forEach((construct) =>
let newconstruct = new Construct()
newconstruct.number = construct.number
newconstruct.name = construct.name
newconstruct.basePrice = construct.basePrice
newconstruct.baseMicrowave = construct.baseMicrowave
newconstruct.atomGain = construct.atomGain
newconstruct.save().then(table.push(newconstruct))
)
console.log(table)
return table
)
// return [ 'test' ]



working around this :



constructFactory.create().then(function (data) 
console.log(data)
)









share|improve this question
















This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



How do I make the .create() function to wait for table to be filled before returning it.
Because data returns undefined



const Construct = require('../models/constructModel')
const TemplateConstruct = require('../models/constructTemplateModel')

exports.create = async function ()
TemplateConstruct.find().then(function (constructs)
let table =
constructs.forEach((construct) =>
let newconstruct = new Construct()
newconstruct.number = construct.number
newconstruct.name = construct.name
newconstruct.basePrice = construct.basePrice
newconstruct.baseMicrowave = construct.baseMicrowave
newconstruct.atomGain = construct.atomGain
newconstruct.save().then(table.push(newconstruct))
)
console.log(table)
return table
)
// return [ 'test' ]



working around this :



constructFactory.create().then(function (data) 
console.log(data)
)




This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers







javascript node.js async-await






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:56









Nikhil Kinkar

363318




363318










asked Nov 11 at 18:06









Lauden

124




124




marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 18:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 18:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • return the promise.
    – Jonas Wilms
    Nov 11 at 18:09
















  • return the promise.
    – Jonas Wilms
    Nov 11 at 18:09















return the promise.
– Jonas Wilms
Nov 11 at 18:09




return the promise.
– Jonas Wilms
Nov 11 at 18:09












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Instead of chaining the promise via .then(), you can await it:



const Construct = require('../models/constructModel');
const TemplateConstruct = require('../models/constructTemplateModel');

exports.create = async function ()
const constructs = await TemplateConstruct.find();
let table = ;

for (const construct of constructs)
let newconstruct = new Construct();
newconstruct.number = construct.number;
newconstruct.name = construct.name;
newconstruct.basePrice = construct.basePrice;
newconstruct.baseMicrowave = construct.baseMicrowave;
newconstruct.atomGain = construct.atomGain;
await newconstruct.save();
table.push(newconstruct);


console.log(table);
return table;
;





share|improve this answer




















  • Thank you, this solved my issue. I understand how async works a little better now!
    – Lauden
    Nov 11 at 18:26

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Instead of chaining the promise via .then(), you can await it:



const Construct = require('../models/constructModel');
const TemplateConstruct = require('../models/constructTemplateModel');

exports.create = async function ()
const constructs = await TemplateConstruct.find();
let table = ;

for (const construct of constructs)
let newconstruct = new Construct();
newconstruct.number = construct.number;
newconstruct.name = construct.name;
newconstruct.basePrice = construct.basePrice;
newconstruct.baseMicrowave = construct.baseMicrowave;
newconstruct.atomGain = construct.atomGain;
await newconstruct.save();
table.push(newconstruct);


console.log(table);
return table;
;





share|improve this answer




















  • Thank you, this solved my issue. I understand how async works a little better now!
    – Lauden
    Nov 11 at 18:26














up vote
1
down vote



accepted










Instead of chaining the promise via .then(), you can await it:



const Construct = require('../models/constructModel');
const TemplateConstruct = require('../models/constructTemplateModel');

exports.create = async function ()
const constructs = await TemplateConstruct.find();
let table = ;

for (const construct of constructs)
let newconstruct = new Construct();
newconstruct.number = construct.number;
newconstruct.name = construct.name;
newconstruct.basePrice = construct.basePrice;
newconstruct.baseMicrowave = construct.baseMicrowave;
newconstruct.atomGain = construct.atomGain;
await newconstruct.save();
table.push(newconstruct);


console.log(table);
return table;
;





share|improve this answer




















  • Thank you, this solved my issue. I understand how async works a little better now!
    – Lauden
    Nov 11 at 18:26












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Instead of chaining the promise via .then(), you can await it:



const Construct = require('../models/constructModel');
const TemplateConstruct = require('../models/constructTemplateModel');

exports.create = async function ()
const constructs = await TemplateConstruct.find();
let table = ;

for (const construct of constructs)
let newconstruct = new Construct();
newconstruct.number = construct.number;
newconstruct.name = construct.name;
newconstruct.basePrice = construct.basePrice;
newconstruct.baseMicrowave = construct.baseMicrowave;
newconstruct.atomGain = construct.atomGain;
await newconstruct.save();
table.push(newconstruct);


console.log(table);
return table;
;





share|improve this answer












Instead of chaining the promise via .then(), you can await it:



const Construct = require('../models/constructModel');
const TemplateConstruct = require('../models/constructTemplateModel');

exports.create = async function ()
const constructs = await TemplateConstruct.find();
let table = ;

for (const construct of constructs)
let newconstruct = new Construct();
newconstruct.number = construct.number;
newconstruct.name = construct.name;
newconstruct.basePrice = construct.basePrice;
newconstruct.baseMicrowave = construct.baseMicrowave;
newconstruct.atomGain = construct.atomGain;
await newconstruct.save();
table.push(newconstruct);


console.log(table);
return table;
;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 18:08









Martin Adámek

7,42641232




7,42641232











  • Thank you, this solved my issue. I understand how async works a little better now!
    – Lauden
    Nov 11 at 18:26
















  • Thank you, this solved my issue. I understand how async works a little better now!
    – Lauden
    Nov 11 at 18:26















Thank you, this solved my issue. I understand how async works a little better now!
– Lauden
Nov 11 at 18:26




Thank you, this solved my issue. I understand how async works a little better now!
– Lauden
Nov 11 at 18:26



這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands