Node JS: Promise resolve value is “undefined”












0















I am trying to save multiple documents(via Mongoose) through a recursive method.



I created a Promise which resolves a method admin_save_choices() to save an Array of documents and finally return an Array of Objects after saving the documents or error message. Along with that when a document is saved in callback it recursively call itself(admin_save_choices()) until Array element exists.



Here is the Promise -



        let choices_object_array_promise = new Promise(function(resolve, reject) {
let choices_object_array = admin_save_choices(choices, timestamp);

resolve(choices_object_array);
});

choices_object_array_promise.then(function(result){

console.log(result);
res.status(200);
res.json('success');
}).catch(function(error) {
res.status(400);
res.json('error');
});


Here is the method -



var admin_save_choices = function(choices, timestamp) {
let choices_object_array = ;

let choice = choices.shift();

if (typeof choice === "undefined")
return choices_object_array;

let choice_information = {
choice: choice,
created_time: timestamp
};

let save_choice_promise = choiceModel.save_choice(choice_information);

save_choice_promise.then(function(choice_result_object) {

choices_object_array.push(choice_result_object);

admin_save_choices(choices, timestamp);

}).catch(function(error) {
return 'error';
});
}


All documents are being saved successfully except I don't get the result come back to choices_object_array_promise.then( callback.



It is showing undefined in console.log(result)



Thanks in advance.










share|improve this question


















  • 1





    Off-topic. Though this is not directly related to the error. Your code contains a few common Promises Antipatterns. The worst one is "The Collection Kerfuffle" Sequential saving looks like this const save = choice=> all => choiceModel.save_choice({choice}).then(item => all.concat(item)); return choices.reduce((saving, item) => saving.then(save(item)), Promise.resolve()

    – Yury Tarabanko
    Nov 22 '18 at 17:54


















0















I am trying to save multiple documents(via Mongoose) through a recursive method.



I created a Promise which resolves a method admin_save_choices() to save an Array of documents and finally return an Array of Objects after saving the documents or error message. Along with that when a document is saved in callback it recursively call itself(admin_save_choices()) until Array element exists.



Here is the Promise -



        let choices_object_array_promise = new Promise(function(resolve, reject) {
let choices_object_array = admin_save_choices(choices, timestamp);

resolve(choices_object_array);
});

choices_object_array_promise.then(function(result){

console.log(result);
res.status(200);
res.json('success');
}).catch(function(error) {
res.status(400);
res.json('error');
});


Here is the method -



var admin_save_choices = function(choices, timestamp) {
let choices_object_array = ;

let choice = choices.shift();

if (typeof choice === "undefined")
return choices_object_array;

let choice_information = {
choice: choice,
created_time: timestamp
};

let save_choice_promise = choiceModel.save_choice(choice_information);

save_choice_promise.then(function(choice_result_object) {

choices_object_array.push(choice_result_object);

admin_save_choices(choices, timestamp);

}).catch(function(error) {
return 'error';
});
}


All documents are being saved successfully except I don't get the result come back to choices_object_array_promise.then( callback.



It is showing undefined in console.log(result)



Thanks in advance.










share|improve this question


















  • 1





    Off-topic. Though this is not directly related to the error. Your code contains a few common Promises Antipatterns. The worst one is "The Collection Kerfuffle" Sequential saving looks like this const save = choice=> all => choiceModel.save_choice({choice}).then(item => all.concat(item)); return choices.reduce((saving, item) => saving.then(save(item)), Promise.resolve()

    – Yury Tarabanko
    Nov 22 '18 at 17:54
















0












0








0








I am trying to save multiple documents(via Mongoose) through a recursive method.



I created a Promise which resolves a method admin_save_choices() to save an Array of documents and finally return an Array of Objects after saving the documents or error message. Along with that when a document is saved in callback it recursively call itself(admin_save_choices()) until Array element exists.



Here is the Promise -



        let choices_object_array_promise = new Promise(function(resolve, reject) {
let choices_object_array = admin_save_choices(choices, timestamp);

resolve(choices_object_array);
});

choices_object_array_promise.then(function(result){

console.log(result);
res.status(200);
res.json('success');
}).catch(function(error) {
res.status(400);
res.json('error');
});


Here is the method -



var admin_save_choices = function(choices, timestamp) {
let choices_object_array = ;

let choice = choices.shift();

if (typeof choice === "undefined")
return choices_object_array;

let choice_information = {
choice: choice,
created_time: timestamp
};

let save_choice_promise = choiceModel.save_choice(choice_information);

save_choice_promise.then(function(choice_result_object) {

choices_object_array.push(choice_result_object);

admin_save_choices(choices, timestamp);

}).catch(function(error) {
return 'error';
});
}


All documents are being saved successfully except I don't get the result come back to choices_object_array_promise.then( callback.



It is showing undefined in console.log(result)



Thanks in advance.










share|improve this question














I am trying to save multiple documents(via Mongoose) through a recursive method.



I created a Promise which resolves a method admin_save_choices() to save an Array of documents and finally return an Array of Objects after saving the documents or error message. Along with that when a document is saved in callback it recursively call itself(admin_save_choices()) until Array element exists.



Here is the Promise -



        let choices_object_array_promise = new Promise(function(resolve, reject) {
let choices_object_array = admin_save_choices(choices, timestamp);

resolve(choices_object_array);
});

choices_object_array_promise.then(function(result){

console.log(result);
res.status(200);
res.json('success');
}).catch(function(error) {
res.status(400);
res.json('error');
});


Here is the method -



var admin_save_choices = function(choices, timestamp) {
let choices_object_array = ;

let choice = choices.shift();

if (typeof choice === "undefined")
return choices_object_array;

let choice_information = {
choice: choice,
created_time: timestamp
};

let save_choice_promise = choiceModel.save_choice(choice_information);

save_choice_promise.then(function(choice_result_object) {

choices_object_array.push(choice_result_object);

admin_save_choices(choices, timestamp);

}).catch(function(error) {
return 'error';
});
}


All documents are being saved successfully except I don't get the result come back to choices_object_array_promise.then( callback.



It is showing undefined in console.log(result)



Thanks in advance.







javascript node.js mongoose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 14:55









user3384985user3384985

1,24211230




1,24211230








  • 1





    Off-topic. Though this is not directly related to the error. Your code contains a few common Promises Antipatterns. The worst one is "The Collection Kerfuffle" Sequential saving looks like this const save = choice=> all => choiceModel.save_choice({choice}).then(item => all.concat(item)); return choices.reduce((saving, item) => saving.then(save(item)), Promise.resolve()

    – Yury Tarabanko
    Nov 22 '18 at 17:54
















  • 1





    Off-topic. Though this is not directly related to the error. Your code contains a few common Promises Antipatterns. The worst one is "The Collection Kerfuffle" Sequential saving looks like this const save = choice=> all => choiceModel.save_choice({choice}).then(item => all.concat(item)); return choices.reduce((saving, item) => saving.then(save(item)), Promise.resolve()

    – Yury Tarabanko
    Nov 22 '18 at 17:54










1




1





Off-topic. Though this is not directly related to the error. Your code contains a few common Promises Antipatterns. The worst one is "The Collection Kerfuffle" Sequential saving looks like this const save = choice=> all => choiceModel.save_choice({choice}).then(item => all.concat(item)); return choices.reduce((saving, item) => saving.then(save(item)), Promise.resolve()

– Yury Tarabanko
Nov 22 '18 at 17:54







Off-topic. Though this is not directly related to the error. Your code contains a few common Promises Antipatterns. The worst one is "The Collection Kerfuffle" Sequential saving looks like this const save = choice=> all => choiceModel.save_choice({choice}).then(item => all.concat(item)); return choices.reduce((saving, item) => saving.then(save(item)), Promise.resolve()

– Yury Tarabanko
Nov 22 '18 at 17:54














1 Answer
1






active

oldest

votes


















2














This is because admin_save_choices do not return anything.



I guess your trying to return the array of admin, maybe this is what you want to do :



// inside admin_save_choices 
return save_choice_promise
.then(function(choice_result_object) {
choices_object_array.push(choice_result_object);
return admin_save_choices(choices, timestamp);
}).catch(function(error) {
return error;
});
}

let choices_object_array_fn = new Promise(function(resolve) {
resolve(admin_save_choices(choices, timestamp));
});


Edit: For anti anti-pattern sake :)






share|improve this answer


























  • @Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

    – Yury Tarabanko
    Nov 22 '18 at 17:33













  • @YuryTarabanko Oh, my bad. I did not see the edit history.

    – Paulpro
    Nov 22 '18 at 17:41






  • 1





    BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

    – Yury Tarabanko
    Nov 22 '18 at 17:45











  • When pattern matters :)

    – Fabien Greard
    Nov 22 '18 at 18:20











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%2f53433579%2fnode-js-promise-resolve-value-is-undefined%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









2














This is because admin_save_choices do not return anything.



I guess your trying to return the array of admin, maybe this is what you want to do :



// inside admin_save_choices 
return save_choice_promise
.then(function(choice_result_object) {
choices_object_array.push(choice_result_object);
return admin_save_choices(choices, timestamp);
}).catch(function(error) {
return error;
});
}

let choices_object_array_fn = new Promise(function(resolve) {
resolve(admin_save_choices(choices, timestamp));
});


Edit: For anti anti-pattern sake :)






share|improve this answer


























  • @Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

    – Yury Tarabanko
    Nov 22 '18 at 17:33













  • @YuryTarabanko Oh, my bad. I did not see the edit history.

    – Paulpro
    Nov 22 '18 at 17:41






  • 1





    BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

    – Yury Tarabanko
    Nov 22 '18 at 17:45











  • When pattern matters :)

    – Fabien Greard
    Nov 22 '18 at 18:20
















2














This is because admin_save_choices do not return anything.



I guess your trying to return the array of admin, maybe this is what you want to do :



// inside admin_save_choices 
return save_choice_promise
.then(function(choice_result_object) {
choices_object_array.push(choice_result_object);
return admin_save_choices(choices, timestamp);
}).catch(function(error) {
return error;
});
}

let choices_object_array_fn = new Promise(function(resolve) {
resolve(admin_save_choices(choices, timestamp));
});


Edit: For anti anti-pattern sake :)






share|improve this answer


























  • @Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

    – Yury Tarabanko
    Nov 22 '18 at 17:33













  • @YuryTarabanko Oh, my bad. I did not see the edit history.

    – Paulpro
    Nov 22 '18 at 17:41






  • 1





    BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

    – Yury Tarabanko
    Nov 22 '18 at 17:45











  • When pattern matters :)

    – Fabien Greard
    Nov 22 '18 at 18:20














2












2








2







This is because admin_save_choices do not return anything.



I guess your trying to return the array of admin, maybe this is what you want to do :



// inside admin_save_choices 
return save_choice_promise
.then(function(choice_result_object) {
choices_object_array.push(choice_result_object);
return admin_save_choices(choices, timestamp);
}).catch(function(error) {
return error;
});
}

let choices_object_array_fn = new Promise(function(resolve) {
resolve(admin_save_choices(choices, timestamp));
});


Edit: For anti anti-pattern sake :)






share|improve this answer















This is because admin_save_choices do not return anything.



I guess your trying to return the array of admin, maybe this is what you want to do :



// inside admin_save_choices 
return save_choice_promise
.then(function(choice_result_object) {
choices_object_array.push(choice_result_object);
return admin_save_choices(choices, timestamp);
}).catch(function(error) {
return error;
});
}

let choices_object_array_fn = new Promise(function(resolve) {
resolve(admin_save_choices(choices, timestamp));
});


Edit: For anti anti-pattern sake :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 18:20

























answered Nov 22 '18 at 15:02









Fabien GreardFabien Greard

1,1641520




1,1641520













  • @Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

    – Yury Tarabanko
    Nov 22 '18 at 17:33













  • @YuryTarabanko Oh, my bad. I did not see the edit history.

    – Paulpro
    Nov 22 '18 at 17:41






  • 1





    BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

    – Yury Tarabanko
    Nov 22 '18 at 17:45











  • When pattern matters :)

    – Fabien Greard
    Nov 22 '18 at 18:20



















  • @Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

    – Yury Tarabanko
    Nov 22 '18 at 17:33













  • @YuryTarabanko Oh, my bad. I did not see the edit history.

    – Paulpro
    Nov 22 '18 at 17:41






  • 1





    BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

    – Yury Tarabanko
    Nov 22 '18 at 17:45











  • When pattern matters :)

    – Fabien Greard
    Nov 22 '18 at 18:20

















@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

– Yury Tarabanko
Nov 22 '18 at 17:33







@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like this new Promise(async function(resolve, reject) and was corrected (I hope due to my comment :)) BTW I still think it would be great to provide antipattern free solution.

– Yury Tarabanko
Nov 22 '18 at 17:33















@YuryTarabanko Oh, my bad. I did not see the edit history.

– Paulpro
Nov 22 '18 at 17:41





@YuryTarabanko Oh, my bad. I did not see the edit history.

– Paulpro
Nov 22 '18 at 17:41




1




1





BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

– Yury Tarabanko
Nov 22 '18 at 17:45





BTW I think the code should be return admin_save_choices(choices, timestamp); to keep the promise chain :)

– Yury Tarabanko
Nov 22 '18 at 17:45













When pattern matters :)

– Fabien Greard
Nov 22 '18 at 18:20





When pattern matters :)

– Fabien Greard
Nov 22 '18 at 18:20


















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%2f53433579%2fnode-js-promise-resolve-value-is-undefined%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'