0

I have two classes as below class AClient

class AClient {
    constructor(AHeaderObj) {
        logger.info("inside tenant client constructor");
        this.tenant_uri = tenantHeaderObj.tenant_uri || config.TENANT_URI;
        this.refresh_token = tenantHeaderObj.refresh_token;
        this.tenant_header = tenantHeaderObj.tenant_header;
    }

Class BClient

class BClient {
    constructor(b_client) {
        logger.info("Inside mongoClient/constructor ");
        this.tenant_client = tenant_client;
    }
async find(db_name, collection_name, selectorQuery) {
        try {
            logger.info("Inside mongoClient find records");
            let dbName = await getDB(db_name, this.tenant_client);
            let db = await getConnection(dbName, this.tenant_client);
            logger.info("selectorQuery"+JSON.stringify(selectorQuery));
            let doc = db.collection(collection_name).find(selectorQuery).toArray();
            logger.info("****************doc**********"+JSON.stringify(doc));
            return await promiseWrapper(null, doc);
        } catch (err) {
            logger.info("Caught error in mongoClient find records ", err);
            return await promiseWrapper(err, null);
        }
    }

    async findSome(db_name, collection_name, query, projection, sort, limit){
        logger.info("Inside mongoClient findSome records");
        try{
            let dbName = await gettDB(db_name, this.tenant_client);
            let db = await getConnection(dbName, this.tenant_client);
            var selector = [{ $match : query},
                { $project : projection},
                { $sort : sort}];
            if(limit > 0){
                selector.push({ $limit : limit})
            }
            let docs = db.collection(collection_name).aggregate(selector).toArray();
            return await promiseWrapper(null, docs);
        } catch (err) {
            logger.info("Caught error in mongoClient findSome records ", err);
            return await promiseWrapper(err, null);
        }
    }

Now I am using it in below 2 ways . One way it works another it is not. Case1: When it is not working

var searchAll = function (type, text, sortBy, sort, Code, AHeaderObj) {
    logger.info("Inside  searchAll  function.");
    var selector = mongoQuery.searchAll(type, text, sortBy, sort, Code);
    var AObj = new AClient(AHeaderObj);
    logger.info("123");
    var mongoObj = new BClient(AObj);
    logger.info("1235");

    var findSome = callbackify(mongoObj.findSome)
    logger.info("12356");

    return new Promise((resolve1, reject1) => {
        logger.info("123567");

        findSome(dBName, collectionName, selector.query, selector.projection, selector.sort, 999999999, function (err1, res1) {
            if (err1) {
                logger.error("Failed to find");
                reject1(err1);
            } else {
                logger.info("Successfully found");
                resolve1(res1)
            }
        });
    })
}

Case2: Where it is working as charm.

var getId = function (id, headerObj, callback) {
    logger.info("inside getAccountById() START:");
    let selector = mongoQuery.GetAccByIdQueryWithIdModelVersion("Id", id, “version", "v2.1")
    let index = 0;
    var AObj = new AClient(AHeaderObj);
        logger.info("123");
        var mongoObj = new BClient(AObj);
        logger.info("1235");
    mongoObj.find(dBName, collectionName, selector).then((result) => {
        logger.info("123568");

        if (result && result.length > 0) {
            logger.info("a");
            logger.info("Found an with id: " + id);
            if (result[index].credentials.length > 0) {
            logger.info("getById() END:"+JSON.stringify(result));
            callback(null, result);
        }else {
            logger.info("b");
            let error = {
                "message": "Unable to fetch the with id: " + id

            };
            logger.warn(error.message);
            logger.info("getById() END:");
            callback(error, null);
        }
    }).catch((err) => {
        logger.info("c");
        logger.info("error" + JSON.stringify(err));
        callback(err, null);
    });
}

Can someone help what am I doing wrong in Case1 . I observed that ion Case 1 object creation is not awaited and db function is been called where I get below error.

Caught error in mongoClient findSome records  TypeError: Cannot read property 'tenant_client' of undefined
2
  • Can you be more specific about what exactly doesn't work? This is a big block of code and we could use some advice about where exactly to look. Commented Oct 22, 2019 at 5:48
  • Both your Aclient and Bclient constructors refer to variables tenantHeaderObj and tenant_client which are not shown any where and do not appear to be using the argument passed to either constructor. This does not look like code that would work in any circumstance. Commented Oct 22, 2019 at 5:51

1 Answer 1

1

Instead of trying to await on class initialization, make the constructor empty and provide and async init method which does the same thing as what the constructor would do.

Make sure that other methods check if the object has been instantiated via the init method and throw an error if not :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.