简体   繁体   中英

node.js/javascript/couchdb view to associative array does not seem to work

I am trying to create a webapp on a node/couchdb/windows stack but get terribly stung by what seems to be a lack of experience.

In the database, there is a view that returns all users with passwords. Based on the tutorial for a blog I have tried to access the view through my node code.

Whenever I investigate the structure of the users or users variable, I get an undefined object.

The call to getDatabase() has been tested elsewhere and works at least for creating new documents.

function GetUser(login) 
{
    var users = GetUsers();
    return users[login];
}

function GetUsers() {
    var db = getDatabase();
    var usersByEmail = [];
    db.view("accounts", "password_by_email")
        .then(function (resp) {
            resp.rows.forEach(function (x) { usersByEmail[x.key] = x.value});
        });
    //usersByEmail['test'] = 'test';
    return usersByEmail;

}

I am aware that both the use of non-hashed passwords as well as reading all users from the database is prohibitive in the final product - just in case anyone wanted to comment on that.

In case something is wrong with the way I access the view: I am using a design document called '_design/accounts' with the view name 'password_by_email'.

Your call to db.view is asynchronous, so when you hit return usersByEmail the object hasn't yet been populated. You simply can't return values from async code; you need to have it make a callback that will execute the code that relies on the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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