NodeJs StrongLoop嵌套回调无法正常工作

时间:2017-03-14 18:55:15

标签: javascript node.js callback loopbackjs strongloop

我已经通过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);

};

请帮我解决这个问题。这是一个回调地狱问题吗?如果是这样,我该如何解决?

1 个答案:

答案 0 :(得分:0)

你正在重新实现很多loopback的功能,同时用很多同步操作来阻止你的服务器。可能多次调用回调函数。没有将错误正确地传递给你的回调。

:)

好消息是,您尝试实现的目标很容易与LB相关。

如果我正确理解您的代码,您只是尝试从自定义URL调用自定义函数。这称为remote method

  

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基础知识。

然后,尝试保持代码简单易读。 如果它没有重构,那就把它缩短,把它拆分成清晰简单的函数。

如果确实需要多次回调,请使用async库进行整理或使用Promises