NodeJS async functions with mongoose [duplicate]












0















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
















0















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














0












0








0








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

620624




620624










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


















1














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









1














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














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












1








1






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,46141232




7,46141232












  • 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



Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation