I'm fairly new to JavaScript and I can't seem to find the answer to this anywhere.
I want to retrieve the JSON returned by a pouchDb get() and be able to use that data outside of the promise function used when querying the db.
Here is what I have/have tried so far:
var row;
db.get(id)
.then(function(doc){
row = JSON.parse(JSON.stringify(doc));
})
.catch(function(err){
console.log(err)
})
console.log(row) //logs undefined
I also randomly for some reason tried this and got unexpected results.
var myArray = []
var row;
db.get(id)
.then(function(doc){
row = JSON.parse(JSON.stringify(doc));
myArray.push(row)
console.log(myArray) //logs Array(1)> 0: Object> my JSON!
console.log(myArray[0]) //logs Object > my JSON!
console.log(myArray[0]._id) //logs My id from my db item
})
.catch(function(err){
console.log(err)
})
console.log(myArray) //logs Array(1) > 0: Object > my JSON!
console.log(myArray[0]) //logs undefined
console.log(myArray[0]._id) //throws Uncaught TypeError: Cannot read property '_id' of undefined + wall of red text
I hope this makes sense--also I may be fundamentally misunderstanding how javascript works.