[英]NodeJs StrongLoop nested callbacks not working properly
我已經用StrongLoop在LoopBack中編寫了一個自定義終結點API,但是它不能正常工作。 起初它可以正常工作,但是在第二或第三次迭代中它不起作用,例如,如果我發布單詞“ API”,它就可以工作,那么如果我搜索“吃”它也可以,但是當我再次發布單詞“ API”時,它就不能工作,即瀏覽器進入循環。 我正在使用嵌套的回調。 這是我的代碼
'use strict';
module.exports = function(app) {
var request = require('request');
var router = app.loopback.Router();
var Verb = app.models.verb;
var Noun = app.models.noun;
var AdditionalTag = app.models.additionalTag;
var responseArray = [];
var verbMatched = false;
var nounMatched = false;
router.get('/api/additionalTagsSuggestions', function(req, res) {
var request = req.query;
if (request.q) {
var query = request.q.split(",")
if (query.length > 0) {
responseArray = [];
var count = 0;
var querySize = query.length;
query.forEach(function(word) {
getTags(word, function(response) {
if (response == true) {
count++;
//console.log("count", count);
if (count == querySize) {
//console.log(responseArray);
if (responseArray.length > 0) {
var temp = [];
responseArray.forEach(function(elem) {
temp.push.apply(temp, elem.tags);
}, this);
temp = temp.filter(function(elem, pos) {
return temp.indexOf(elem) == pos;
})
console.log(temp);
responseArray = temp;
temp = [];
res.send(responseArray);
} else {
responseArray = [];
//console.log(responseArray);
res.send(responseArray);
}
}
} else {
count++;
if (count == querySize) {
responseArray = [];
res.send(responseArray);
}
}
})
}, this);
}
} else {
responseArray = [];
res.send(responseArray);
}
});
function getTags(word, callback) {
Verb.find(function(err, verbs) {
if (err) {
//console.log("error finding verb");
} else {
if (verbs.length > 0) {
verbs[0]["verbs"].forEach(function(verb) {
if (verb == word) {
verbMatched = true;
getNouns(word, function(response) {
callback(response);
});
}
});
if (!verbMatched) {
verbMatched = false;
getNouns(word, function(response) {
callback(response);
});
}
} else {
verbMatched = false;
getNouns(word, function(response) {
callback(response);
});
}
}
})
}
function getNouns(word, callback) {
if (verbMatched) {
Noun.find({ "fields": { "noun": 1 }, "where": { "verbs": word } }, function(err, arr) {
if (arr) {
if (arr.length > 0) {
nounMatched = true;
getAdditionalTags(arr, function(response) {
callback(response);
});
} else {
callback(false);
}
} else {
callback(false);
}
});
} else if (!verbMatched) {
Noun.find({ fields: { noun: true }, where: { noun: word } }, function(err, arr) {
if (arr) {
if (arr.length > 0) {
nounMatched = true;
getAdditionalTags(arr, function(response) {
callback(response);
});
} else {
nounMatched = false;
getAdditionalTags(word, function(response) {
callback(response);
});
}
} else {
callback(false);
}
});
}
}
function getAdditionalTags(arr, callback) {
if (nounMatched) {
var arrSize = arr.length;
var count = 0;
arr.forEach(function(obj) {
AdditionalTag.find({ fields: { tags: true }, where: { noun: obj.noun } }, function(err, tags) {
if (tags) {
if (tags.length > 0) {
count++;
tags.forEach(function(tag) {
responseArray.push(tag);
}, this);
if (count == arrSize) {
console.log("lala");
//console.log(responseArray);
callback(true);
}
} else {
count++;
if (count == arrSize) {
console.log("asdfas");
callback(false);
}
}
} else {
callback(false);
}
})
}, this);
} else if (!nounMatched) {
AdditionalTag.find({ fields: { noun: true }, where: { tags: arr } }, function(err, nouns) {
if (nouns) {
if (nouns.length > 0) {
nounMatched = true;
getAdditionalTags(nouns, function(response) {
callback(response);
});
} else {
callback(false);
}
} else {
callback(false);
}
})
}
}
app.use(router);
};
請在這個問題上幫助我。 這是一個回調地獄問題嗎? 如果是這樣,我該如何解決?
您正在重新實現許多回送功能,同時通過大量阻塞服務器的同步操作來實現它。 可能多次調用回調函數。 沒有正確地將錯誤傳遞給您的回調。
:)
好消息是,您要實現的目標是使用LB輕松實現。
如果我能正確理解您的代碼,則您只是在嘗試從自定義URL調用自定義函數。 這稱為遠程方法 。
在
model/AdditionalTag
這是一個示例代碼,可以完成90%的代碼(例如拆分查詢參數,驗證輸入等)。
module.exports = function(additionalTag){
additionalTag.suggestion = function(param1, param2, cb) {
// Put your custom logic here, where you identify additional tags from the parameters and your application data
// You DON'T need to manually separate the query fields anymore using ','
// Loopback does that for you, just use param1, param2, etc.
// Then call back to send the http response
// null means no error was found
// here the remote method returns a string. You can return anything else, like a json object, array, number, etc.
cb(null, 'Additional tags found.');
}
// Here you register the function additionalTag.suggestion as a remotely callable one
additionalTag.remoteMethod('suggestion', {
accepts: [
{arg: 'param1', type: 'string'},
{arg: 'param2', type: 'string'}
],
returns: {arg: 'message', type: 'string'}
});
};
為了您的最大利益,您應該多學習javascript,而無需返回環回。 這是一個很棒的框架,但是它需要一些JS的基礎知識。
然后,嘗試使您的代碼簡單易讀。 如果不是,則將其重構,使其更短,然后將其拆分為清晰簡單的函數。
如果確實需要多個回調,請使用異步庫或Promises進行組織 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.