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.










share|improve this question


















  • 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

















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.










share|improve this question


















  • 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















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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 with Promise.all.
    – Alex Blex
    Nov 7 at 15:33
















  • 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










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



















active

oldest

votes











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',
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%2f53191355%2fwhat-is-the-scope-of-mongodb-collection-method-callback-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini