我試圖打破基於表達式的功能,但是在確定範圍時遇到了麻煩。下面是代碼片段:執行回調中的作用域
function createService(dict, res) {
// Ensure no duplicate
var serviceExists = Service.find({name: dict['name']}).count().exec(function(err, doc) {
return (doc !== 0);
});
console.log(serviceExists);
if(serviceExists){
res.send(500, "Duplicate document. Try updating existing entry or chooseing a different name");
return;
}
//Insert the document
service = new Service(dict);
service.save(function(err) {
if(!err) {
res.send("Service saved");
}
});
}
中的console.log()的輸出:
{ emitted: {},
_events: { err: [Function], complete: [Function] } }
這裏的最終目標是,代碼將無法達到「插入文件」部分如果doc!== 0.請讓我知道這樣做的正確方法(也許使用異常?這是我唯一的想法)。謝謝
在尋找如何將額外值傳遞給回調函數時,我得到了這個結果。我發現如何做到這一點,並在這裏發佈:http://stackoverflow.com/a/28120741/1695680 – ThorSummoner 2015-01-24 00:05:49