0

I have been spinning my wheels for hours and hours. I've read all the pouchdb docs and a dozen promise tutorials. I'm new to javascript and to Pouchdb and I'm missing something. I would really appreciate it if someone could provide an example on how I can do this. I have a bunch of documents in PouchDB. I am trying to pull out a document into a local object so I can edit it, and then put it back into the database. I cant figure out how.

return this.calendarDatabase.get(date).then(function (doc) {
return doc }).then(function (doc) {

console.log(doc);

console.log shows something like:

Object {
_id : "2016-10-12T05:00:00.000Z"
_rev: "3-827f6ef7faf9aa2e70999fe628cefba9"
date: "2016-10-12T05:00:00.000Z"
Recipes: Array[3]
...
}

So I am able to access the document correctly. But I can't pull anything out for use. A simple example:

return this.calendarDatabase.get(date).then(function (doc) {
return doc }).then(function (doc) {

console.log(doc);

this.name = doc._id;
console.log(name);
}

I get an error:

TypeError: Cannot set property 'name' of null(…)

I've realized that I get the same error even if I do this.name = "String", so there is something about the promise that I don't get.

Chris Hudson
  • 5
  • 1
  • 4
  • Why are you setting anything to `this` inside the promise? – Dave Newton Oct 29 '16 at 16:29
  • That `.then(function (doc) { return doc })` should be completely omitted. – Bergi Oct 29 '16 at 17:48
  • Notice that the idea of promises is to *never pull anything out*. Trying to access the value directly is like asking to predict the future. Instead, produce another future value by chaining `then` onto it, and store the resulting *promise* somewhere. Even if you'd succeed to set the `.name` property of your instance (by fixing the callback's `this` context), you'd never know *when* it is save to access the name already or whether the fetching is still ongoing (or has failed). – Bergi Oct 29 '16 at 17:50

0 Answers0