10.3. JavaScript development tips
Working with Apache CouchDB’s JavaScript environment is a lot different thanworking with traditional JavaScript development environments. Here are sometips and tricks that will ease the difficulty.
- Remember that CouchDB’s JavaScript engine is old, only supporting theECMA-262 5th edition (“ES5”) of the language. ES6/2015 and newer constructscannot be used.
Fortunately, there are many tools available for transpiling modern JavaScriptinto code compatible with older JS engines. The Babel Project website, for example, offers an in-browser text editorwhich transpiles JavaScript in real-time. Configuring CouchDB-compatibilityis as easy as enabling the ENV PRESET
option, and typing “firefox 4.0”into the Browsers field.
The
log()
function will log output to the CouchDB log file or stream.You can log strings, objects, and arrays directly, without first convertingto JSON. Use this in conjunction with a local CouchDB instance for bestresults.Be sure to guard all document accesses to avoid exceptions when fieldsor subfields are missing:
if (doc && doc.myarray && doc.myarray.length)…
原文: http://docs.couchdb.org/en/stable/best-practices/jsdevel.html