Node JS: Promise resolve value is “undefined”
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 17:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 14:55
user3384985
1,22211230
1,22211230
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 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 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 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 at 17:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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 at 17:33
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 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 at 17:45
When pattern matters :)
– Fabien Greard
Nov 22 at 18:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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 at 17:33
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 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 at 17:45
When pattern matters :)
– Fabien Greard
Nov 22 at 18:20
add a comment |
up vote
2
down vote
accepted
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 at 17:33
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 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 at 17:45
When pattern matters :)
– Fabien Greard
Nov 22 at 18:20
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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 at 18:20
answered Nov 22 at 15:02
Fabien Greard
1,0941520
1,0941520
@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 at 17:33
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 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 at 17:45
When pattern matters :)
– Fabien Greard
Nov 22 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 at 17:33
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 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 at 17:45
When pattern matters :)
– Fabien Greard
Nov 22 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 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 at 17:33
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 at 17:41
@YuryTarabanko Oh, my bad. I did not see the edit history.
– Paulpro
Nov 22 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 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 at 17:45
When pattern matters :)
– Fabien Greard
Nov 22 at 18:20
When pattern matters :)
– Fabien Greard
Nov 22 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 at 17:54