node.js - 为什么这个 coffeescript 代码总是返回 true?

标签 node.js coffeescript redis

当我发布到服务器时,无论我向 auth 函数提供什么信息,它都会返回 true。我的直觉是我正在尝试同步做一些事情,这本质上是异步的,但我不知道如何修复它。

auth = (username, api_key, device) ->
  hashed_key = hash.sha256(username + api_key + device, salt) 
  winston.debug('Checking auth for ' + username)
  redis_client.get hashed_key, (err, data) ->
    if data == username
        true

# Main Handler for posting data for a device.
server.post "/:customer/:site/:device", create = (req, res, next) ->
    message = JSON.parse(req.body)
    winston.info(server.name + ': Recieved event from ' + req.params.device)
    authenticated = auth(message.username, message.api_key, message.device)
    winston.debug('****' + authenticated)
    if authenticated == true 
        winston.debug('Auth passed, got a valid user/device/api combination: ' + message.username)
        redis_client.publish('device_events', req.body)
        return next()
    else
        winston.debug('Auth failed, cant find device ' + message.device + ' for ' + message.username)
        return next(restify.NotAuthorizedError)

最佳答案

如果您知道(或有预感)某些东西是异步的,您应该将之后要执行的操作作为回调函数传递。我不确定你的服务器的 post 函数是如何工作的,但如果它像 Node HTTP's request您应该执行以下操作:

get = (location, callback, retriever, filterer, formatter)->
  decoratedCallback = (data)->
    callback formatter.applyFormat filterer.applyFilter data
  retriever.retrieve location, decoratedCallback

module.exports = get

关于node.js - 为什么这个 coffeescript 代码总是返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10379688/

相关文章:

node.js - GitLab 依赖扫描需要源代码中的 package-lock.json 才能执行

node.js - 在子目录中将 Gulp 应用程序部署到 Heroku

node.js - Coffeescript - 如何调用作用域链上两层的方法?

python - 使用 Python 将 SQL 查询结果存储到 Redis 中?

node.js - kubernetes中redis和nodejs出错

node.js - 错误 : ERR value is not an integer or out of range from redis. 调用 ('zcard' ,'myzset' ) 在 LUA 脚本中

javascript - 无法在 Express JS 中设置单独的路由文件

css - 如果找到特定字符串,则有条件地添加一个类 Rails CoffeeScript

javascript - Coffeescript,我将如何编写这个排队函数示例,尤其是循环?

node.js - 获取 mongoDB API Node Express 中 3 个集合中的所有文档