Akmal
Akmal

Reputation: 142

How to filter replication between pouchdb and couchdb

I have couchdb on server side and pouchdb for my mobile application. I want each user get their own data based on the reference number(ref_no). I already tried to filter it but there are no data sync to couchdb. I follow the step from pouchdb/couchdb documentation. But I dont know whether the step that I follow is correct or not. Please guide me if I make a mistake. Below is my code in client side.

constructor(public http: Http, public settingProvider:SettingProvider) {

  this.db = new PouchDB('task');

}


initializeRemote(url,ref_no){

  this.remote = url + '/task';

  let options = {
    live: true,
    retry: true,
    filter:'task/byRef_no',
    query_params: {'ref_no':ref_no}
  };

  this.db.sync(this.remote, options)
    .on('change', function(change){
      console.log('InspectionTask provider change!', change);
    })
    .on('paused', function(info){
      console.log('InspectionTask provider paused!', info);
    })
    .on('active', function(info){
      console.log('InspectionTask provider active!', info);
    })
    .on('error', function(err){
      console.log('InspectionTask provider error!', err)
    });
}

And this is filter in server side.

{
  _id: '_design/task',
  filters: {
    myfilter: function (doc, req) {
      return doc.ref_no === req.query.ref_no;
    }
  }
}

Upvotes: 1

Views: 637

Answers (1)

Alexis Côté
Alexis Côté

Reputation: 3690

The filter that you specified in your PouchDB replication configuration isn't not the one in your design document.

You're trying to filter with : filter:'task/byRef_no'.

Actually, your filter name is myfilter.

You should have something looking like this :

let options = {
    live: true,
    retry: true,
    filter:'task/myfilter',
    query_params: {'ref_no':ref_no}
  };

Upvotes: 1

Related Questions