简体   繁体   English

NodeJS res.write无法正常工作

[英]NodeJS res.write doesn't work console works

I'm trying to use Nodejs to create a REST JSON but when I try to write, it gives me error can anyone help? 我正在尝试使用Nodejs创建REST JSON,但是当我尝试编写时,它给了我错误,有人可以帮忙吗?

var oracle = require ( 'oracle' ) ;
var http = require ( 'http' ) ;
var url = require ('url');
var port = 80 ;
var testarray = []; 

var cnnString = {
   hostname        : 'xxx.xxx.xxx.xxx',
   port            : xxxxx,
   database        : 'xxxxxxxxxx.com' ,
   user            : 'xxxxx' ,
   password        : 'xxxxxx'
} ;


oracle.connect ( cnnString, countrylist ) ;


function countrylist( err, connection ) {

   var reader = connection.reader("select distinct company_code, company_name from mbl_employee_v order by company_code", []);

function doRead(cb) {
reader.nextRow(function(err, row) {
    if (err) return cb(err);
    if (row) {
        // do something with row

        testarray.push(JSON.stringify(row));

        return doRead(cb)
    } else {
        // we are done
        return cb();
    }
})
}

doRead(function(err) {
if (err) throw err; // or log it
console.log("all records processed");
 // console.log(testarray);
});
}



var app  = http.createServer ( cb ) ;
app.listen ( port , function () {
console.log ( 'Http server listening at port : ' + port ) ;
} ) ;

function cb ( req, res ) {  
var QueryVal = url.parse(req.url,true);
res.writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;

switch ( QueryVal.pathname ) {
    case '/' :
        res.write ( "Hello, World" ) ;
        break ;
    case '/about' :

console.log(testarray); 
res.write (testarray) ;

        break ;


    case '/product' :
    res.write ( 'product' ) ;

        break ;         
    default :
        res.write ( 'unknow' ) ;
        break ;
}

res.end ( '' ) ;
} ;

here my console.log shows the data, but the res.write gives me error 这里我的console.log显示了数据,但是res.write给我错误

http.js:851 throw new TypeError('first argument must be a string or Buffer'); http.js:851抛出新的TypeError(“第一个参数必须是字符串或Buffer”); ^ TypeError: first argument must be a string or Buffer ^ TypeError:第一个参数必须是字符串或Buffer

  • at ServerResponse.OutgoingMessage.write (http.js:851:11) 在ServerResponse.OutgoingMessage.write(http.js:851:11)
  • at Server.cb(/opt/Nodejs/ex05-readData01.js:65:7) 在Server.cb(/opt/Nodejs/ex05-readData01.js:65:7)
  • at Server.emit(events.js:98:17) 在Server.emit(events.js:98:17)
  • at HTTPParser.parser.onIncoming (http.js:2108:12) 在HTTPParser.parser.onIncoming(http.js:2108:12)
  • at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23) 在HTTPParser.parserOnHeadersComplete [as onHeadersComplete](http.js:121:23)
  • at Socket.socket.ondata (http.js:1966:22) 在Socket.socket.ondata(http.js:1966:22)
  • at TCP.onread (net.js:527:27) 在TCP.onread(net.js:527:27)

Do this 做这个

res.write(JSON.stringify(testArray));

instead of 代替

res.write(testArray);

because write function takes a String or Buffer as argument, and you are passing an array. 因为write函数将String或Buffer作为参数,并且您正在传递数组。

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

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