I am new to Node.js. I wrote this code:
我是新手。我写的这段代码:
db.query("SELECT * FROM messages", function (error, results, fields){
if(error){
console.log("test");
}
if(results.length > 0) {
console.log(results);
}
});
But it doesn't work, it doesn't print anything to console. Running on Linux.
但是它不能工作,它不能打印任何东西来控制台。在Linux上运行。
1 个解决方案
#1
0
For your particular question, do not put your results in an if
statement yet. Do this instead, just to see what you're working with:
对于您的特定问题,不要将结果放在if语句中。这样做,看看你在做什么:
console.log( results.toString() );
console.log( JSON.stringify( results ) ); //sometimes this throws errors, if the object is complex
That should let you see what you're working with. If reulsts
isn't an array, it won't have a .length
attribute, and you won't reach the console.log
.
这会让你知道你在做什么。如果reulsts不是数组,那么它就没有.length属性,您也无法访问console.log。
However...
然而……
I'm always pushing KnexJS when I see raw MySQL statements in Node code. Knex handles two common things for you, db connections and connection pools, plus makes writing dynamic queries a breeze. (as in, you don't have to manipulate SQL strings...)
当我在节点代码中看到原始的MySQL语句时,我总是推KnexJS。Knex为您处理两件常见的事情:db连接和连接池,以及编写动态查询。(比如,您不必操纵SQL字符串……)
Install knex npm install knex --save
安装knex npm安装knex——保存
In your app or environment setup
在你的应用程序或环境设置中。
var Knex = require('knex');
Knex.knex = Knex({...}); //set options for DB
In other files, reference the instance like this:
在其他文件中,引用实例如下:
var knex = require('knex).knex;
So your query could look like this:
你的查询可能是这样的:
var query = knex('messages').select();
console.log('About to perform query: ' + query.toString() );
query.exec( function( err, results ){
if(error){
console.log("test");
}
console.log(results);
//do other things here, like callback(results);
} );
Good luck!
好运!
#1
0
For your particular question, do not put your results in an if
statement yet. Do this instead, just to see what you're working with:
对于您的特定问题,不要将结果放在if语句中。这样做,看看你在做什么:
console.log( results.toString() );
console.log( JSON.stringify( results ) ); //sometimes this throws errors, if the object is complex
That should let you see what you're working with. If reulsts
isn't an array, it won't have a .length
attribute, and you won't reach the console.log
.
这会让你知道你在做什么。如果reulsts不是数组,那么它就没有.length属性,您也无法访问console.log。
However...
然而……
I'm always pushing KnexJS when I see raw MySQL statements in Node code. Knex handles two common things for you, db connections and connection pools, plus makes writing dynamic queries a breeze. (as in, you don't have to manipulate SQL strings...)
当我在节点代码中看到原始的MySQL语句时,我总是推KnexJS。Knex为您处理两件常见的事情:db连接和连接池,以及编写动态查询。(比如,您不必操纵SQL字符串……)
Install knex npm install knex --save
安装knex npm安装knex——保存
In your app or environment setup
在你的应用程序或环境设置中。
var Knex = require('knex');
Knex.knex = Knex({...}); //set options for DB
In other files, reference the instance like this:
在其他文件中,引用实例如下:
var knex = require('knex).knex;
So your query could look like this:
你的查询可能是这样的:
var query = knex('messages').select();
console.log('About to perform query: ' + query.toString() );
query.exec( function( err, results ){
if(error){
console.log("test");
}
console.log(results);
//do other things here, like callback(results);
} );
Good luck!
好运!