Can't push array in function [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers



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

    33 answers




I know this has been asked before but I can't seem to find the answer, my array can't push to datahasil, how to push array hasil to datahasil...??






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], function(err, result) {
var datahasil =
result.forEach(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
}, )
datahasil.push(hasil)
})
});





enter image description here










share|improve this question















marked as duplicate by Grégory NEUT, CertainPerformance 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 9 at 8:30


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.















  • suggestion: consider not to query database in a loop.
    – scetiner
    Nov 9 at 8:26










  • theres no return value
    – nyx97
    Nov 9 at 8:27










  • forEach + async is virtually never a good combination
    – Bravo
    Nov 9 at 8:29










  • Use a debugger...
    – James
    Nov 9 at 8:31

















up vote
-1
down vote

favorite













This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers



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

    33 answers




I know this has been asked before but I can't seem to find the answer, my array can't push to datahasil, how to push array hasil to datahasil...??






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], function(err, result) {
var datahasil =
result.forEach(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
}, )
datahasil.push(hasil)
})
});





enter image description here










share|improve this question















marked as duplicate by Grégory NEUT, CertainPerformance 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 9 at 8:30


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.















  • suggestion: consider not to query database in a loop.
    – scetiner
    Nov 9 at 8:26










  • theres no return value
    – nyx97
    Nov 9 at 8:27










  • forEach + async is virtually never a good combination
    – Bravo
    Nov 9 at 8:29










  • Use a debugger...
    – James
    Nov 9 at 8:31















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers



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

    33 answers




I know this has been asked before but I can't seem to find the answer, my array can't push to datahasil, how to push array hasil to datahasil...??






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], function(err, result) {
var datahasil =
result.forEach(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
}, )
datahasil.push(hasil)
})
});





enter image description here










share|improve this question
















This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers



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

    33 answers




I know this has been asked before but I can't seem to find the answer, my array can't push to datahasil, how to push array hasil to datahasil...??






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], function(err, result) {
var datahasil =
result.forEach(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
}, )
datahasil.push(hasil)
})
});





enter image description here





This question already has an answer here:




  • Using async/await with a forEach loop

    12 answers



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

    33 answers







const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], function(err, result) {
var datahasil =
result.forEach(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
}, )
datahasil.push(hasil)
})
});





const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], function(err, result) {
var datahasil =
result.forEach(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
}, )
datahasil.push(hasil)
})
});






javascript node.js express mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 8:26









Ayush Gupta

3,0011642




3,0011642










asked Nov 9 at 8:23









Ahmad Sayadi

376




376




marked as duplicate by Grégory NEUT, CertainPerformance 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 9 at 8:30


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 Grégory NEUT, CertainPerformance 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 9 at 8:30


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.














  • suggestion: consider not to query database in a loop.
    – scetiner
    Nov 9 at 8:26










  • theres no return value
    – nyx97
    Nov 9 at 8:27










  • forEach + async is virtually never a good combination
    – Bravo
    Nov 9 at 8:29










  • Use a debugger...
    – James
    Nov 9 at 8:31




















  • suggestion: consider not to query database in a loop.
    – scetiner
    Nov 9 at 8:26










  • theres no return value
    – nyx97
    Nov 9 at 8:27










  • forEach + async is virtually never a good combination
    – Bravo
    Nov 9 at 8:29










  • Use a debugger...
    – James
    Nov 9 at 8:31


















suggestion: consider not to query database in a loop.
– scetiner
Nov 9 at 8:26




suggestion: consider not to query database in a loop.
– scetiner
Nov 9 at 8:26












theres no return value
– nyx97
Nov 9 at 8:27




theres no return value
– nyx97
Nov 9 at 8:27












forEach + async is virtually never a good combination
– Bravo
Nov 9 at 8:29




forEach + async is virtually never a good combination
– Bravo
Nov 9 at 8:29












Use a debugger...
– James
Nov 9 at 8:31






Use a debugger...
– James
Nov 9 at 8:31














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You gave forEach function an asynchronous method removing its synchronous nature so instead, use map and await Promise.all on the resulting array:






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});








share|improve this answer























  • Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
    – CertainPerformance
    Nov 9 at 8:31




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You gave forEach function an asynchronous method removing its synchronous nature so instead, use map and await Promise.all on the resulting array:






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});








share|improve this answer























  • Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
    – CertainPerformance
    Nov 9 at 8:31

















up vote
0
down vote



accepted










You gave forEach function an asynchronous method removing its synchronous nature so instead, use map and await Promise.all on the resulting array:






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});








share|improve this answer























  • Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
    – CertainPerformance
    Nov 9 at 8:31















up vote
0
down vote



accepted







up vote
0
down vote



accepted






You gave forEach function an asynchronous method removing its synchronous nature so instead, use map and await Promise.all on the resulting array:






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});








share|improve this answer














You gave forEach function an asynchronous method removing its synchronous nature so instead, use map and await Promise.all on the resulting array:






const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});








const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});





const data1 = await JadwalBooking.aggregate([{
$project: {
"keterangan": "$keterangan",
"jumlah_tersisa": "$datalayout.jumlah_kursi_tersisa",
"flag": "$flag",
"date1": "$datamisa.date",
}
},
{
$match: {
flag: 1
},
},

], async function(err, result) {
const datahasil = await Promise.all(result.map(async(item) => {
const hasil = await DetailBookingMisa.find({
$or: [{
flag: 3
},
{
flag: 4
}
],

$and: [{
jadwalbookingmisa: item._id
}]
})
return hasil;
});
console.log(datahasil);
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 8:32

























answered Nov 9 at 8:29









dotconnor

1,037120




1,037120












  • Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
    – CertainPerformance
    Nov 9 at 8:31




















  • Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
    – CertainPerformance
    Nov 9 at 8:31


















Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
– CertainPerformance
Nov 9 at 8:31






Rather than pushing to an external array, probably better to just use the results of the Promise.all (one less variable, more pure)
– CertainPerformance
Nov 9 at 8:31





這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

JBPM : POST request for execute process go wrong