Node JS: Promise resolve value is “undefined”
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
add a comment |
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
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 thisconst 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
add a comment |
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
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
javascript node.js mongoose
asked Nov 22 '18 at 14:55
user3384985user3384985
1,32721330
1,32721330
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 thisconst 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
add a comment |
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 thisconst 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
add a comment |
1 Answer
1
active
oldest
votes
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 :)
@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like thisnew 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 bereturn 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 :)
@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like thisnew 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 bereturn 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
add a comment |
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 :)
@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like thisnew 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 bereturn 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
add a comment |
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 :)
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 :)
edited Nov 22 '18 at 18:20
answered Nov 22 '18 at 15:02
Fabien GreardFabien Greard
1,2391520
1,2391520
@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like thisnew 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 bereturn 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
add a comment |
@Paulpro The comment was about a new code in the answer itself. Check the edit history it was like thisnew 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 bereturn 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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