Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

How to return a value from a PouchDB database?

$
0
0

I am working in a React-Native project (JavaScript language) with a PouchDB database. A have a specific file for database functions and one of them is:

export function getStratumName(columnId,layerKey){
    const db  = new PouchDB('lithodex')
    const current_user_id = 'admin'

    db.get('default').then(function(database){           
        const index_current_user = database.users.findIndex(element => element._id === current_user_id);
        const index_column       = database.users[index_current_user].columns
                                    .findIndex(element => element.columnId === columnId);  
        const index_layer        = database.users[index_current_user].columns[index_column].layerList
                                    .findIndex(element => element.key === layerKey); 

        name = database.users[index_current_user].columns[index_column].layerList[index_layer].stratumName;
        console.log('>>>>>> 1 - The value is: '+name);
        return name;

    });
    //console.log('>>>>>> 2 - The value is: '+name);
    //return name;
}

The variable that I want to return is "name", and I also verified that it gets the correct value in the first console.log (which has the number 1). However, once I invoke this function in another file, I get the value "undefined".

The reason for this should be related to the fact that the variable "name" is only under the scope of the part "db.get('default').then(function(database)". I noticed this because I added the two external lines that in the example are commented, and it says that it can't find the variable "name". Moreover, it is corroborated by the fact that if I initialize "name" at the beginning of the function with any value, like 'foo', and I also leave the external "return" that now is commented, then when I invoke the function I get 'foo'.


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>