2014-03-06 20 views
0

完整的代碼和錯誤我得到下面我真的很想讓輸出正常工作所以我可以在我的代碼在移動async和nodes js函數console.log接收NaN,NaN,Nan需要幫助將字符串輸出爲字符串而不是數字

var app = require('express')() 
      , server = require('http').createServer(app) 
      , fs = require('fs') 
      , exec = require('child_process').exec 
      , io = require('socket.io').listen(server); 

var async = require('async'); 

server.listen(process.env.PORT); 

app.get('/', function (req, res) { 
    res.sendfile(__dirname + '/client/index.html'); 
    // console.log(async); 
}); 

io.sockets.on('connection', function (socket) { 
    //socket.emit('dinpu', { hello: 'world' }); 
    // myModule('[email protected]:asdfef'); 
    socket.on('dout', function (data) { 
    //module2(data); 
    //console.log(JSON.parse(data.message)); 
    for(i = 0; i < data.message.length; i++) { 
     //data.message[i] = data.message[i].replace(/"/g, ""); 
    } 
    module1(data.message); 
    module2(data.message); 

    function module1(data) { 
     console.log(data); 
    } 


    function async(arg, callback) { 
     console.log('do something with \''+arg+'\', return 1 sec later'); 
     setTimeout(function() { callback(arg * 2); }, 1000); 
    } 

    function module2(data) { 
     function final() { console.log('Done', results); } 
     var items = data; 
     var results = []; 
     function series(item) { 
      if(item) { 
       async(item, function(result) { 
        results.push(result); 
        return series(items.shift()); 
       }); 
      } else { 
       return final(); 
      } 
     } 
     series(items.shift()); 
    } 


}); 
}); 

一切工作正常,除了這部分

function final() { console.log('Done', results); } 

我得到的結果

do something with 'dfhfgh', return 1 sec later 
do something with 'gfhjghj', return 1 sec later 
do something with 'gfhjghj', return 1 sec later 
do something with 'fghjgh', return 1 sec later 
do something with 'ghjfhj', return 1 sec later 
Done [ NaN, NaN, NaN, NaN, NaN ]<<< this is the error I need it to output the strings 

任何幫助修復我的代碼我真的很感激它:)。

我已經在這之後沒有了許多有益的意見已經得到解決我仔細一看後,我設法得到這個工作

所以糾正我的節點服務器之後,我現在得到繼承人正確的結果正確的代碼和結果如下

var app = require('express')() 
, server = require('http').createServer(app) 
, fs = require('fs') 
, exec = require('child_process').exec 
, io = require('socket.io').listen(server); 
var async = require('async'); 
server.listen(process.env.PORT); 

app.get('/', function (req, res) { 
res.sendfile(__dirname + '/client/index.html'); 
// console.log(async); 
}); 

io.sockets.on('connection', function (socket) { 
    //socket.emit('dinpu', { hello: 'world' }); 
// myModule('[email protected]:asdfef'); 
socket.on('dout', function (data) { 
//module2(data); 
//console.log(JSON.parse(data.message)); 
for(i = 0; i < data.message.length; i++) { 
//data.message[i] = data.message[i].replace(/"/g, ""); 

} 
module1(data.message); 
module2(data.message); 

function module1(data) { 
    console.log(data); 
} 


function async(arg, callback) { 
    console.log('do something with \''+arg+'\' return 1 sec later'); 
    setTimeout(function() { callback('\''+arg+'\''); }, 1000); 
} 

    function module2(data) { 
    function final() { console.log('Done', '\''+results+'\''); } 
    var items = data; 
    var results = []; 
    function series(item) { 
         if(item) { 
     async(item, function(result) { 
       results.push(result); 
       return series(items.shift()); 
      }); 
     } else { 
      return final(); 
     } 
    } 
    series(items.shift()); 
} 


}); 
}); 

結果

do something with 'sdfg' return 1 sec later 
do something with 'srg' return 1 sec later 
do something with 'sdf' return 1 sec later 
Done ''sdfg','srg','sdf'' 
+0

什麼你期望它輸出? – Gntem

+0

[dfhfgh,gfhjghj,gfhjghj,fghjgh,ghjfhj] – user3385289

+1

然後嘗試刪除「* 2」部分.. – Gntem

回答

0

setTimeout(function() { callback(arg * 2); }, 1000);

這一行,arg不是一個數字,你不能使用* 2

+0

我可以使用什麼來代替數字呢?我明白你的意思我只是不認爲這是一個很好的答案我如何解決這個問題的一個例子將是偉大的 – user3385289

+0

@ user3385289它是不可能知道如何解決它。你希望用'2'來把一個字符串乘以什麼? –

+0

我在問如何解決這個問題,我不需要乘以參數它的一個明顯的錯誤,我想要做的就是輸出[dfhfgh,gfhjghj,gfhjghj,fghjgh,ghjfhj]我不需要任何其他的東西我已經使用一個例子來幫助我構建這個代碼,所以我要求正確的方式來修改它,所以我只是輸出字符串 – user3385289

相關問題