wrap couchbase access function

arachide

below is my couchbase nodejs code

kdatabase.js

 var couchbase = require('couchbase');
 var db = new couchbase.Connection({
         host: "http://127.0.0.1:8091",
         bucket: "default",
     },
     function(err) {
         if (err) throw err;

         db.get('id1', function(err, result) {

             if (err) throw err;
             console.log(result.value);
             process.exit(0);

         });

     });

it works

but I hope to wrap it to object that can be easily to operate

module.exports = function(app) {
    return new KDatabase(app);
};
var KDatabase = function(app) {
    this.app = app;
};
//couchbase
KDatabase.prototype.query = function(userName) {

    var couchbase = require('couchbase');
    var db = new couchbase.Connection({
            host: "http://127.0.0.1:8091",
            bucket: "default",
        },
        function(err) {
            if (err) throw err;
            console.log(userName + '!!!!--');
            db.get(userName, function(err, result) {
                if (err) throw err;
                var o = result.value;
                console.log(o['password'] + '***--');
                return o['password'];
            });
        });
};

then I call

var db = require('kdatabase.js')();
var s = db.query(msg.username, function(err) {

        if (err) {

            console.log('aaa');
        }
        console.log('bbb');
        return;
    });

the lines

console.log(userName + '!!!!--');

console.log(o['password'] + '***--');

display correctly

but

console.log('aaa');

console.log('bbb');

are never executed

Explosion Pills

Your query method does not take a callback argument, so you never call it.

KDatabase.prototype.query = function(userName, cb) {
    /* snip */
    console.log(o['password'] + '***--');
    cb(err, result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related