Javascript node.js-if else-string for-res.end()
my index.js(昵称:1.js) my/2.jsJavascript node.js-if else-string for-res.end(),javascript,node.js,Javascript,Node.js,my index.js(昵称:1.js) my/2.js exports.makeQuery = function(connection, callback) { var queryString = 'SELECT * FROM 1_accounts order by ac_nu asc limit 5'; connection.query(queryString, function(err,res,fields){ if (err) {return callback(err
exports.makeQuery = function(connection, callback) {
var queryString = 'SELECT * FROM 1_accounts order by ac_nu asc limit 5';
connection.query(queryString, function(err,res,fields){
if (err) {return callback(err)};
bb = JSON.stringify(res);
callback(null, bb);
});
}
错误:
root@11669:/# node 1.js
_http_outgoing.js:524
throw new TypeError('first argument must be a string or Buffer');
^
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:524:11)
at Server.<anonymous> (/1.js:38:6)
at emitTwo (events.js:87:13)
at Server.emit (events.js:172:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:528:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)
root@11669:/#
root@11669:/#节点1.js
_http_outgoing.js:524
抛出新的TypeError('第一个参数必须是字符串或缓冲区');
^
TypeError:第一个参数必须是字符串或缓冲区
在ServerResponse.OutgoingMessage.end(_http_outgoing.js:524:11)
在服务器上。(/1.js:38:6)
两点钟(events.js:87:13)
在Server.emit(events.js:172:7)
在HTTPParser.parserOnIncoming[作为onIncoming](\u http\u server.js:528:12)
在HTTPParser.parserOnHeadersComplete(_http_common.js:88:23)
root@11669:/#
问题位于index.js(1.js)末尾的某个位置就在“else”语句之后 我不知道怎么纠正它 我不认为其他任何地方存在冲突。
2.js应该是正常的 更新:
错误发生在我将其加载到web后您正在使用1.js中的异步函数makeQuery,因此您应该在完成makeQuery函数后将数据传递到浏览器。试试这个
if (o == '0'){
o = 'page not found';
res.end(o);
}else{
o.makeQuery(connection, function(err, result){
if(err) return res.end(err);
o = result;
res.end(o)
});
}
也许我无法从o.makeQuery下将o设置为“result”的位置向res.end()发送任何内容。换句话说,我可能无法在此时设置“o”的值。您进行了异步调用,然后使用对象调用res.end,因此错误有没有一种方法可以在不要求回拨或错误报告的情况下进行调用,以便我可以完全删除。。函数(err,result){if(err)返回res.end(err);
if (o == '0'){
o = 'page not found';
res.end(o);
}else{
o.makeQuery(connection, function(err, result){
if(err) return res.end(err);
o = result;
res.end(o)
});
}