简体   繁体   English

如何将变量传递到书架功能?

[英]How to pass variables into Bookshelf function?

I am creating a web app and am using bookshelf to query one of my databases. 我正在创建一个Web应用程序,并且正在使用书架来查询我的数据库之一。 However, I want to get some information from my database and put it into an array, but I don't know how to pass the array into my then function. 但是,我想从数据库中获取一些信息并将其放入数组中,但是我不知道如何将数组传递给then函数。 How do I do this? 我该怎么做呢?

Here is my current code: 这是我当前的代码:

var courses = [];
Bookshelf.knex('users').where('email', req.session.user.email).then(function(users) {
    users.forEach(function(user) {
        courses.push(user);
    });
});
console.log(courses);

When I try to do this, nothing gets put into courses. 当我尝试执行此操作时,课程没有任何设置。 Could anyone tell me how to allow the bookshelf call to access the courses variable? 谁能告诉我如何允许书架调用访问课程变量?

Thanks in advace. 非常感谢。

since this involves async operation you can't get the result in sync fashion. 由于这涉及异步操作,因此您无法以同步方式获取结果。 You should be doing something like this 你应该做这样的事情

var courses = [];
Bookshelf.knex('users').where('email', req.session.user.email).then(function(users) {
    users.forEach(function(user) {
        courses.push(user);
    });
}).then(() => console.log(courses));

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

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