On 04/22/2010 03:44 PM, Niels Egberts wrote:
> I'm creating an application to manage projects. The idea is that you
> can add todo-lists, files, notes, etc.
Hi Niels, that sounds awesome! Is the project hosted on launchpad or
somewhere else, or is it too early to ask that question yet? :)
> I found out that deleted items
> were not really deleted, but only marked as such. So I want to create
> a view that returns only non-deleted objects.
>
> What I tried is this:
> function(doc) {
> if (doc.application_annotations["Ubuntu One"]
> ["private_application_annotations"]["deleted"]) {
> emit(null, doc);
> }
> }
> This returns the deleted items as expected.
>
> Then I tried this:
> function(doc) {
> if (!doc.application_annotations["Ubuntu One"]
> ["private_application_annotations"]["deleted"]) {
> emit(null, doc);
> }
> }
> But this returns nothing... why?
>
> Then I tried to be smart and tried this:
> function(doc) {
> if (doc.application_annotations["Ubuntu One"]
> ["private_application_annotations"]["deleted"]) {
> } else {
> emit(null, doc);
> }
> }
> But this does not return anything too. Can anyone explain why?
You're almost there, but the problem is that your code checks the
value of the deleted field, and considers the document "not deleted"
when that value is false. This is correct but incomplete, since most
documents that are not deleted will not have this field at all.
In desktopcouch itself, this is handled as follows:
function(doc) {
try {
if (! doc['application_annotations']['Ubuntu One']
['private_application_annotations']['deleted']) {
emit(doc.record_type, doc);
}
} catch (e) {
emit(doc.record_type, doc);
}
}
getting the value of a property that does not exist will throw an
error, at which point the couchdb view emits nothing for that
record. In this code, we catch that error, and say: if the field is
not even there, that must mean the record is not deleted.
Hope that helps,
--
eric casteleijn
https://code.launchpad.net/~thisfred
Canonical Ltd.