【发布时间】:2014-06-29 15:21:28
【问题描述】:
我创建了一个带有“_id”、“name”和“status”字段的 pouchDB 文档。但是,我发现如果我想更新文档的“状态”字段,那么我还需要指定我没有更改的所有其他字段,否则它们会被删除。也就是说,如果我在更新时不指定“名称”字段,则文档上将不再有“名称”字段。
function createFriendSchedule(friend){
pouchDB.put({
_id : friend['id'],
name : friend['name'],
status: "new"
}, function(err, response){
createAndDispatchEvent("friend Schedule created");
});
}
这是更新文档的代码
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") }
});
});
}
这是用于提取记录的代码
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");
});
如果我没有在更新时指定“名称”字段,则 pouchDB.query 中的 emit() 调用返回的数组包含一个空值,我希望“名称”值是这个值。
【问题讨论】:
标签: javascript pouchdb