서버에 게시 할 때 auth 함수에 제공 한 정보에 관계없이 true를 반환합니다. 나의 직감은 내가 동기로 무언가를하려고 시도한다는 것인데, 그것은 본질적으로 비동기이지만, 그것을 고치는 방법을 모른다.이 coffeescript 코드가 항상 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)
auth에 false를 추가하면 그 동작이 변경됩니까? – Menztrual
아니요 - 아무런 차이가 없으므로 auth가 함수 선언으로 true이지만 실행되지 않거나 시간 내에 완료되지 않는다고 생각하게 만듭니다 ...... –
직감이 정확합니다. *'redis_client.get'가 실행되기 전에'auth' 함수가 * 리턴합니다. 그래서 콜백이 필요합니다. 더 이상 같은 범위에 있지 않기 때문에 콜백에서 돌아올 수 없습니다. 비슷한 상황에 대한 [내 대답] (http://stackoverflow.com/questions/9310855/get-and-get-value/9310916#9310916) 및 [이 답변] (http://stackoverflow.com/ 왜이 패러다임을 사용하는지에 대한 설명을 위해 질문/9362823/why-is-a-function-and-a-callback-in-non-blocking-in-node-js/9363071 # 9363071) –