javascriptnode.jscouchdb

How to check if a document exists by ID in CouchDB with Node.js


I am using the node-couchdb library for node.js and its APIs are listed here.

However, I can't seem to find a valid API that checks if a particular document ID already exists in a database. Most of them are about creating, updating, or deleting documents from a database. Are there any possible solutions to checking if a document exists?

I have tried using the couch.get() API, which gets the data in the document specified by an ID. It didn't seem to work either as it resulted in error if the document doesn't exist.


Solution

  • Requesting a document (whether a HEAD or a GET) and getting either a 200 or 404 response IS the way to check whether a resource exists in an HTTP environment, and that's how couch works.