自定义函数来检查文档是否存在于CouchDB nodejs nano中
我一直在Google上寻找答案,根据find的结果, 我可以使用nano模块来检查一个表是否存在于CouchDB中。
但是,当我尝试使它成为一个自定义函数时,无论如何它总是返回“undefined”。 这是function:
var exists = function( id ) { this.head( id, function( err, body, header ) { if ( header[ 'status-code' ] == 200 ) return true; else if ( err[ 'status-code' ] == 404 ) return false; return false; }); }
叫它:
nano.db.create( 'databaseName', function() { var users = nano.use( 'databaseName' ); console.log( exists.call( users, 'documentToCheck' ) ); });
这到底是什么错误? 我似乎无法弄清楚。
您的函数存在返回未定义,因为内部匿名函数返回您需要的值。
治疗这种疾病是为了反映你的function存在
var exists = function( id , cb) { this.head( id, function( err, body, header ) { if ( header[ 'status-code' ] == 200 ) cb(true); else if ( err[ 'status-code' ] == 404 ) cb(false); cb(false); }); }
用法:
exists.call(users, 'documentToCheck', function(check) { console.log(check); });