简体   繁体   中英

Check if PouchDB document is empty

I am working on an application that stores client demographics and health information. My goal is to be able to append something to an existing document and I need to check if a document with a certain ID already exists. I know about the function: putIfNotExists() but I need to be able to check this with an if statement because I have other conditions.

Isn't there anyway I can check if a document tied to a specific ID exists?

I was trying this:

    if (db.get('my_id') !== "") {
        // do something
    }

But while this works it also breaks my code because when I try pushing documents based on some user input it executes the above if block when it shouldn't.

This might not be the best answer but it accomplishes what I want.

I retrieve my document using an id and I check the error status:

db.get(id, function(err, resp) {
    if (err) {
        if (err.status = '404') { // if the document does not exist
            // do something
        }
        else {
       // do something else if document exists
       }
    }
}

try this :

db.get('mydoc1', function(err, resp) {
      if (err) {
         console.log("does not exist");
       } else {
        console.log("document exists");
      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM