1

    const connectToDB = async uri => {
   if (cachedDb) {
      return Promise.resolve(cachedDb);
   }
   return MongoClient.connect(config[STAGE].dbUrls[uri], { useNewUrlParser: true, useUnifiedTopology: true }).then(db => {
      cachedDb = db;
      return cachedDb;
   });
};

exports.handler = (event, context, callback) => {
   console.log("[Find All] [event] : ", event);
   context.callbackWaitForEmptyEventLoop = false;
   connectToDB('MONGODB_URI_FIND').then(database => {
      let db = database.db(config[STAGE].dbUrls.DB_NAME);
      console.log("db connection  ", db);
      fetchDataFromDb(db, event, callback);

   });
};

const fetchDataFromDb = (db, event, callback) => {
   const { table, query } = event;
   const options = { ...query.options };
   delete query.options;
   const { sortFields, limit, skip } = options || {};
   db.collection(table).find(query, options).sort(sortFields || {}).limit(limit || 999).skip(skip || 0).toArray((error, result) => {
      console.log("Fetched data[] : ", { error, result });
      if (error)
         callback(null, { body: error });
      else
         callback(null, { body: result });
   });
};

In the above code, the callback(null,{}) function is not executing, Code is working when I am using return or context.done() instead of callback(). please help me to execute callback. thanks in advance

1 Answer 1

1

Have to use

context.callbackWaitsForEmptyEventLoop = false

instead

context.callbackWaitForEmptyEventLoop = false;
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.