I create a pouchDB document with "_id", "name", and "status" fields. However, I find that if I want to update the "status" field of the document then I need to also specify all the other fields I am NOT changing as well otherwise they get erased. That is, if I do not specify the "name" field on update, there will no longer be a "name" field on the document.
function createFriendSchedule(friend){
pouchDB.put({
_id : friend['id'],
name : friend['name'],
status: "new"
}, function(err, response){
createAndDispatchEvent("friend Schedule created");
});
}
This is the code for updating the document
function changeFriendStatus(friend){
pouchDB.get(friend['id'], function(err, retrieved){
pouchDB.put({
_id : friend['id'],
_rev : retrieved._rev, //need to specify _rev otherwise conflict will occur
name : retrieved.name, //if you don't specify name should remain the samme as before, then it will be left off the record!
status : friend['status']
}, function(err, response){
if(err){
console.log("COULDN'T CHANGE FRIEND STATUS");
} else { createAndDispatchEvent("friend status changed") }
});
});
}
And here is the code used to pull the record out
window.pouchDB.query(
{map : map},
{reduce : false},
function(err, response){
var responseRows = response['rows'];
var cleanedList = [];
_.each(responseRows, function(friend){
cleanedList.push({'_id' : friend['key'][0], 'name' : friend['key'][1], 'status' : friend['key'][2] });
});
window.reminderList = cleanedList;
console.log(window.reminderList);
createAndDispatchEvent("Returned reminder list");
});
If I don't specify the "name" field on update, the array returned by the emit() call in pouchDB.query contains a null value where I expect the "name" value to be.