NodeJS async functions with mongoose [duplicate]
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)
})
javascript node.js async-await
marked as duplicate by Jonas Wilms
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.
add a comment |
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)
})
javascript node.js async-await
marked as duplicate by Jonas Wilms
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
add a comment |
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)
})
javascript node.js async-await
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
javascript node.js async-await
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
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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;
};
Thank you, this solved my issue. I understand how async works a little better now!
– Lauden
Nov 11 at 18:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
};
Thank you, this solved my issue. I understand how async works a little better now!
– Lauden
Nov 11 at 18:26
add a comment |
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;
};
Thank you, this solved my issue. I understand how async works a little better now!
– Lauden
Nov 11 at 18:26
add a comment |
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;
};
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;
};
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
add a comment |
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
add a comment |
return
the promise.– Jonas Wilms
Nov 11 at 18:09