javascript 如何在javascript中使用knex选择*?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20603800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do select * from using knex in javascript?
提问by user1896766
I have a function like this:
我有一个这样的功能:
function get_projects() {
var project_names=[];
knex('projects').select('name').then(function (a) {
project_names.push(a);
})
return project_names;
}
This function executes the return statement return project_names;
before completing project_names.push(a)
statement and what I get after calling this function is an empty array, even though my db has results (I can see this if i log within the function a())
这个函数return project_names;
在完成语句之前执行 return 语句project_names.push(a)
,调用这个函数后我得到的是一个空数组,即使我的数据库有结果(如果我在函数 a() 中登录,我可以看到这个)
回答by Dewey
knex select() returns a promise so that you can continue the flow within then() function.
knex select() 返回一个 promise,以便您可以在 then() 函数中继续流程。
knex('projects').select('name').then(function(projectNames){
//do something here
console.log(projectNames);
});
回答by clay
Nodejs is asynchronous, so as soon as your line knex('projects').select('name').then...
is executed, the line `return project_names;' is run. As you have found out, it didn't wait until it was filled with values!
Nodejs 是异步的,所以只要你的代码行knex('projects').select('name').then...
被执行,`return project_names;' 行 正在运行。正如您所发现的,它并没有等到它充满了值!
The standard "return" at the end of the methods isn't a good programming style for Nodejs, which is event based. It has its place, but more common is a callback approach. Consider reading some tutorials on this new approach (I like this one)
方法末尾的标准“返回”不是基于事件的 Nodejs 的良好编程风格。它有它的位置,但更常见的是回调方法。考虑阅读一些关于这种新方法的教程(我喜欢这个)
You might change your current code to something like:
您可以将当前代码更改为以下内容:
function print_project_names() {
get_projects( function(names){
for( var i = 0; i < names.length; i++ )
{
console.log(names[i]+'\n');
}
});
}
function get_projects( callback ) {
var project_names=[];
knex('projects').select('name').then(function (a) {
project_names.push(a);
callback(project_names);
})
}
NOTE: This is not optimized code
注意:这不是优化代码
Here, when you want to print the project names (unsure your real goal), you are passing an actual function definition, as the "callback", into "get_projects". As the events fire, once the results are pushed into the project names, the callback is called with your new list.
在这里,当您想要打印项目名称(不确定您的真正目标)时,您将一个实际的函数定义作为“回调”传递到“get_projects”中。当事件触发时,一旦将结果推送到项目名称中,就会使用您的新列表调用回调。
回答by jemiloii
I know this is old, but you can do this:
我知道这是旧的,但你可以这样做:
function get_projects() {
return knex('projects').select('name')
.then(function (records) {
// already returns an array you can do other things here
return records;
})
}
Now you can do this to access the data.
现在您可以执行此操作来访问数据。
get_projects()
.then(function (records) {
// do what you need as well, like send to the frontend using express
res.send(records);
})