【问题标题】:JSHint gives Expected an identifier and instead saw a functionJSHint 给 Expected 一个标识符,而是看到一个函数
【发布时间】:2016-05-19 03:25:27
【问题描述】:

我已经声明了一个查询 Redis 的函数,如果在 redis 中没有找到数据,则查询 SQL 数据库。以下是我的代码,它在第一行本身给出了错误。我不确定为什么作为参数的函数会导致麻烦。

function redusId(taskId, function (err, reply){
var status = taskId + ".status";
var response = taskId + ".response";
var jsonObject = {};

redisClient.get(status, function (error, reply) {
    if (error) {
        console.log("redis.status.ERROR: " + error);
        return;
    }

    if (!reply) {
        checkSQLdb(taskId, function (error, data) {
            if(error) {
                console.log("sql.ERROR", error);
            }

            if(!data) {
                console.log("sql.status.ERROR");
            }
            else {
                // data retrieval and posting in redis and calling redis client again
                var id = data[0].id;
                var status = data[0].status;
                var response = data[0].response;
                console.log(id, status, response);
                var requestid = id + ".status";
                redisClient.set(requestid, status);
                requestid = id + ".response";
                redisClient.set(requestid, response);
                redusId(id);
            }
        })
      }
    else {
        jsonObject."status" = reply;

        if (reply == 1) {
                //redis returns non one status no response is expected
                redisClient.get(response, function(error, reply) {
                    if (error) {
                        //redis has the status but not the response
                        console.log("redis.response.ERROR ", error);
                    }
                    else {
                        jsonObject."response" = response;
                        return jsonObject;
                    }
                });
             }
             else {
                console.log("status is not one ; no response is expected");
                return jsonObject;
             }
        }
    })
});

【问题讨论】:

  • 您可以在调用函数时而不是在声明函数时将函数作为参数。
  • 基本上当你声明一个函数时,它应该看起来像这样function redusId(taskId,callback){...},当你像这样调用它时redusId(1, function(){...});
  • @Molda 谢谢,它解决了这个问题。
  • @SaurabhAriyan 如果您在 cmets 中找到了答案,并且没有人发布该答案作为答案,那么在此发布您自己的答案并接受它是公平的游戏点,这样我们就可以结束 JSLint 标记上的问题。

标签: node.js jslint jshint redisclient


【解决方案1】:

这是在 cmets 中指出的,没有人发布作为答案,因此现在发布。

代码 sn-p 中的问题是我试图声明函数并将其作为参数传递。在给定的代码sn-p中,正确的做法

    function redusId(taskId,callback){...}

然后将其称为

    redusId(1, function(){...});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2016-12-31
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多