updating pouchdb local document raising 409 and not reflecting changes in database but in revisions

269 views
Skip to first unread message

Kiflemariam Andom

unread,
Jul 20, 2019, 4:45:14 AM7/20/19
to Angular and AngularJS discussion
Hi Sander and All,

Hope your weekend is doing well. Can someone help me please with pouchdb please. It is driving me nuts. You can see my code here https://stackblitz.com/edit/angular-akrcz9

What happens is I can have any number of app settings so I might update just one field of the document. And the update can happen from two, three places at the same time since the app have differnet activities that happen at the back. My prefered option was to use localStorage but the client wants all to be stored in pouchdb. I run into 409 error but do see the changes in the revisions, just not in the document itself.


import { Injectable } from "@angular/core";


import PouchDB from "pouchdb";

@Injectable()
export class AppService {
 
private settingsDb; //to store app state

  constructor
() {
   
// indicates if we are in the process of syncing or not.
   
this.settingsDb = new PouchDB("field-support-app-state", {
      auto_compaction
: true
   
});
 
}

  updateAppState
(fields) {
   
/*
      Create a document if it doesnt exist else update it
      if creating a new, set all to default then update as
      necessary.

      Fields is dictionary of fields to update
      e.g.
        {
            'field1':'field1 value'
        }
   
      we can update the fields we want only
        */


    let vm
= this;

   
const docId = "_local/appState";

   
this.settingsDb.get(docId, function(err, doc) {
     
if (err) {
       
// cant get the info so create the document now with all the fields
        console
.log("config read error; possibly not found ", err);
        let data
= { _id: docId };
        data
["field1"] = "";
        data
["field2"] = "";
        data
["field3"] = "";

        data
["lastNewDataDownloadedDisplayedOn"] = "";
       
//replace as necessary now
       
Object.keys(fields).forEach(function(key) {
          data
[key] = fields[key];
       
});
        vm
.settingsDb.put(data);
     
} else {
       
// document exists already
       
// so make changes only then put back the whole document

       
Object.keys(fields).forEach(function(key) {
          doc
[key] = fields[key];
       
});

        vm
.settingsDb.put(doc);
     
}
   
});
 
}
}


The method would be called from number of places with the field needed for an update. To illustrate, this is from component.ts:

import { Component } from '@angular/core';
import {AppService} from './app.service';

@Component({
  selector
: 'my-app',
  templateUrl
: './app.component.html',
  styleUrls
: [ './app.component.css' ]
})
export class AppComponent  {
  name
= 'Angular';
    constructor
(
   
public appSerivce: AppService
 
) {

           
this.appSerivce.updateAppState({
          lastSyncErrorDisplayedon
: ""
       
})

 
this.randomUpdates();
 
}



  randomUpdates
(){
    let fields
= ['field1', 'lastUpdatedLocationOn','lastItemMovedOn'];
   
const vm : any = this;
    fields
.forEach(field=>{

      setInterval
(
        vm
.appSerivce.updateAppState({
        field
: new Date().toLocaleString()
     
}) ,
       
       
3000);

     
   
});
 
}




}


What I want is to be able to update fields of the document in a flexible way. As the app grows, there is a chance a specific omponent or feature might want to save its own setting and i don't want to create document for each one tho that is also possible if absolutellly necessary.

Sander Elias

unread,
Jul 21, 2019, 12:03:06 AM7/21/19
to Angular and AngularJS discussion
Hi Kiflemariam,

Hmm, this seems more an issue of pouchDB, I did take a quick look, but as a 1-time pouchDB user (4+ years ago), I could not find an answer. My suggestion would be, ask it in their support communities. 

Regards
Sander

Tito

unread,
Jul 21, 2019, 11:31:02 AM7/21/19
to Angular and AngularJS discussion

ክፍሎም ሐወይ documentation አንብባ !


https://pouchdb.com/guides/conflicts.html


Immediate conflicts

Immediate conflicts can occur with any API that takes a rev or a document with _rev as input – put()post()remove()bulkDocs(), and putAttachment(). They manifest as a 409(conflict) error:

Reply all
Reply to author
Forward
0 new messages