What is the scope of MongoDB collection method callback parameters?
up vote
0
down vote
favorite
I am wondering if the error
and result
parameters in nested MongoDB queries need different names than those used by their parent/s.
The following is just an example of a nested scenario's structure, not an actual set of updates.
var filter_1 = { _id: o_id };
var update_1 = { $set: { title: title } };
var options_1 = {};
collection.findOneAndUpdate(filter_1, update_1, options_1, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
// BEGIN perform a subsequent update
var filter_2 = { _id: o_id };
var update_2 = { $set: { genre: genre } };
var options_2 = {};
collection.findOneAndUpdate(filter_2, update_2, options_2, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
res.json( { result: result } );
}
});
// END perform a subsequent update
}
});
Can both the first and second update use:
function(error, result) {
if (error) {
res.send(error);
} else {...
Or does the nested update need to be something like:
function(error_2, result_2) {
if (error_2) {
res.send(error_2);
} else {...
I am assuming it is the former, and error
and result
can be used in both updates, as the parameters would be restricted to their own function's scope, but just thought I would check.
node.js mongodb
add a comment |
up vote
0
down vote
favorite
I am wondering if the error
and result
parameters in nested MongoDB queries need different names than those used by their parent/s.
The following is just an example of a nested scenario's structure, not an actual set of updates.
var filter_1 = { _id: o_id };
var update_1 = { $set: { title: title } };
var options_1 = {};
collection.findOneAndUpdate(filter_1, update_1, options_1, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
// BEGIN perform a subsequent update
var filter_2 = { _id: o_id };
var update_2 = { $set: { genre: genre } };
var options_2 = {};
collection.findOneAndUpdate(filter_2, update_2, options_2, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
res.json( { result: result } );
}
});
// END perform a subsequent update
}
});
Can both the first and second update use:
function(error, result) {
if (error) {
res.send(error);
} else {...
Or does the nested update need to be something like:
function(error_2, result_2) {
if (error_2) {
res.send(error_2);
} else {...
I am assuming it is the former, and error
and result
can be used in both updates, as the parameters would be restricted to their own function's scope, but just thought I would check.
node.js mongodb
1
The second one shadows the former. It's better to use async/await instead of callback pyramid. If the second update doesn't depend on the first one - run them in parallel withPromise.all
.
– Alex Blex
Nov 7 at 15:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am wondering if the error
and result
parameters in nested MongoDB queries need different names than those used by their parent/s.
The following is just an example of a nested scenario's structure, not an actual set of updates.
var filter_1 = { _id: o_id };
var update_1 = { $set: { title: title } };
var options_1 = {};
collection.findOneAndUpdate(filter_1, update_1, options_1, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
// BEGIN perform a subsequent update
var filter_2 = { _id: o_id };
var update_2 = { $set: { genre: genre } };
var options_2 = {};
collection.findOneAndUpdate(filter_2, update_2, options_2, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
res.json( { result: result } );
}
});
// END perform a subsequent update
}
});
Can both the first and second update use:
function(error, result) {
if (error) {
res.send(error);
} else {...
Or does the nested update need to be something like:
function(error_2, result_2) {
if (error_2) {
res.send(error_2);
} else {...
I am assuming it is the former, and error
and result
can be used in both updates, as the parameters would be restricted to their own function's scope, but just thought I would check.
node.js mongodb
I am wondering if the error
and result
parameters in nested MongoDB queries need different names than those used by their parent/s.
The following is just an example of a nested scenario's structure, not an actual set of updates.
var filter_1 = { _id: o_id };
var update_1 = { $set: { title: title } };
var options_1 = {};
collection.findOneAndUpdate(filter_1, update_1, options_1, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
// BEGIN perform a subsequent update
var filter_2 = { _id: o_id };
var update_2 = { $set: { genre: genre } };
var options_2 = {};
collection.findOneAndUpdate(filter_2, update_2, options_2, function(error, result) {
if (error) {
res.send(error);
} else {
console.log(result);
res.json( { result: result } );
}
});
// END perform a subsequent update
}
});
Can both the first and second update use:
function(error, result) {
if (error) {
res.send(error);
} else {...
Or does the nested update need to be something like:
function(error_2, result_2) {
if (error_2) {
res.send(error_2);
} else {...
I am assuming it is the former, and error
and result
can be used in both updates, as the parameters would be restricted to their own function's scope, but just thought I would check.
node.js mongodb
node.js mongodb
asked Nov 7 at 14:23
user1063287
3,4111557126
3,4111557126
1
The second one shadows the former. It's better to use async/await instead of callback pyramid. If the second update doesn't depend on the first one - run them in parallel withPromise.all
.
– Alex Blex
Nov 7 at 15:33
add a comment |
1
The second one shadows the former. It's better to use async/await instead of callback pyramid. If the second update doesn't depend on the first one - run them in parallel withPromise.all
.
– Alex Blex
Nov 7 at 15:33
1
1
The second one shadows the former. It's better to use async/await instead of callback pyramid. If the second update doesn't depend on the first one - run them in parallel with
Promise.all
.– Alex Blex
Nov 7 at 15:33
The second one shadows the former. It's better to use async/await instead of callback pyramid. If the second update doesn't depend on the first one - run them in parallel with
Promise.all
.– Alex Blex
Nov 7 at 15:33
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53191355%2fwhat-is-the-scope-of-mongodb-collection-method-callback-parameters%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
The second one shadows the former. It's better to use async/await instead of callback pyramid. If the second update doesn't depend on the first one - run them in parallel with
Promise.all
.– Alex Blex
Nov 7 at 15:33