简体   繁体   English

奇怪的iterpritation coffeescript到javascript。 为什么?

[英]Strange iterpritation coffeescript to javascript. Why?

I'm performing get request in coffee script: 我正在执行咖啡脚本中的获取请求:

parseRequest = (line) ->
     try
       request = JSON.parse(line)
     catch err
     try
    switch request.method
        when 'authorize'
            req = http.get { port: 8080, path: "/test/config" }, (res) ->
            console.log "Get response: #{res.statusCode}"
            res.on 'data', (chunk) -> 
                console.log('body: ' + chunk)
                request.params[0] = JSON.parse(chunk).config.username
                console.log "Request: " + request.params[0]
                request.params[1] = JSON.parse(chunk).config.password
                console.log "Request: " + request.params[1]
                line = JSON.stringify(request)
                console.log "Changed username and pass to: #{request.params[0]},#{request.params[1]}"
                ......{lot of code}.........
                return line

In output I get this: 在输出中我得到这个:

 Changed username and pass to: original,p
 Get response: 200
 body: {"config":{"username":"newuser","password":"newpass"}}
 Request: newuser
 Request: newpass

So last log message output first and in result of function, that is store in 'line', still contains old values. 因此,最后一个日志消息的输出首先是函数的结果,即存储在“行”中,仍然包含旧值。 Of course, I try a lot of different variants, in attempts to fix this problem, but get no luck. 当然,为了解决这个问题,我尝试了许多不同的方法,但没有成功。 Then I decide to look at compiled javascript based on this coffee script. 然后,我决定基于此coffee脚本查看已编译的javascript。 I see this: 我看到这个:

req = http.get({
    port: 8080,
    path: "/test/config"
}, function(res) {
    console.log("Get response: " + res.statusCode);
    return res.on('data', function(chunk) {
        console.log('body: ' + chunk);
        request.params[0] = JSON.parse(chunk).config.username;
        console.log("Request: " + request.params[0]);
        request.params[1] = JSON.parse(chunk).config.password;
        return console.log("Request: " + request.params[1]);
     });
 });
 line = JSON.stringify(request);
 console.log("Changed username and pass to: " + request.params[0] + "," + request.params[1]);

In java script last two lines, was moved out of scope function on('data'), that's why I recive last output in first line, and that's why variable line contains old values. 在Java脚本的最后两行中,移出了作用域函数on('data'),这就是为什么我在第一行中获取最后的输出,这就是变量行包含旧值的原因。 But why coffee script translate my script to such javascript code? 但是,为什么咖啡脚本将我的脚本转换为此类javascript代码? Something connected with visibility of variable line? 与可变线的可见性相关的东西? How can I fix it and get what I excpected in coffeescript? 我该如何解决它并得到我在Coffeescript中期望的结果?

The problem was in mixing spaces and tabs. 问题出在混合空格和制表符。 All lines that should be in function has 6 tabs equal indentation, but in all strings that was include in function, there were 4 spaces between 3rd and 5th tab, so it looks like tab, but was not so. 应该在函数中的所有行都有6个等于缩进的制表符,但是在函数中包括的所有字符串中,第3个和第5个制表符之间有4个空格,因此看起来像制表符,但并非如此。 Meanwhile two last strings have only tabs in indentation. 同时,最后两个字符串的缩进仅包含制表符。 That was a problem of coping some string from other source. 这是应对其他来源的字符串的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM