Yannick Pezeu
Yannick Pezeu

Reputation: 818

IBM Cloud Functions: Use async calls in Nodejs

I run this code on Cloud.ibm.com in an Action accessing a Cloudant Database:

 async function main(params) {
     const cloudant = Cloudant({
         url: params.COUCH_URL,
         plugins: { iamauth: { iamApiKey: params.IAM_API_KEY } }
     });
 
 
     try {
         
         let dbList = await cloudant.db.list();
         let db = await cloudant.use('dealerships')
         let result = await db.get("ddb5ed4f65d21bf4a4da409b8bc4b70a")
         
         let query = {
           "selector": {
              "_id": "ddb5ed4f65d21bf4a4da409b8bc4b70a"
           },
           "fields": [
              "_id",
              "_rev"
           ],
           "sort": [
              {
                 "_id": "asc"
              }
           ]
        }
         let result2 = await db.find(query, function(err, data){
            return data;
         });
         
         let indexes_result = await db.index(function(err, result) {
          if (err) {
            throw err;
          }
          else{
              return result
          }
         let indexes_ = {};
          
          return indexes_

        });
         
         return {
             "dblist": dbList,
             "result": result,
             "result2": result2,
         }
         
          } catch (error) {
         return { error: error.description };
     }
 
 }

My result is the following:

Results:
{
  "dblist": [
    "alice",
    "celina",
    "dealerships",
    "gaetan",
    "guestbook",
    "reviews"
  ],
  "result": {
    "_id": "ddb5ed4f65d21bf4a4da409b8bc4b70a",
    "_rev": "1-34e7ebd07643af43db578a46ee1d6365",
    "address": "3 Nova Court",
    "city": "El Paso",
    "full_name": "Holdlamis Car Dealership",
    "id": 1,
    "lat": 31.6948,
    "long": -106.3,
    "short_name": "Holdlamis",
    "st": "TX",
    "state": "Texas",
    "zip": "88563"
  }
}
Logs:
[]

So my result2 is absent from the result object and I have no clue on what is going on. I whish I could access some logs and some errors to know where the problem was but I don't have access to anything.

Can someone please provide me a method to access the logs and the errors ?

Upvotes: 0

Views: 483

Answers (2)

data_henrik
data_henrik

Reputation: 17156

Your IBM Cloud Functions action can make use of asynchronous code. For Node.js check out this Functions documentation on how to fix it. You need to use a Promise.

You can see the activation logs for your action in the dashboard or retrieve them using the CLI / API or via SDK.

BTW: In your own answer your are referring to Cloudant documentation. It is independent of IBM Cloud Functions / OpenWhisk.

Upvotes: 1

Yannick Pezeu
Yannick Pezeu

Reputation: 818

I figured out that the functions that I was trying to use like this:

let result2 = await db.find(query, function(err, data){
            return data;
         });
         
         let indexes_result = await db.index(function(err, result) {
          if (err) {
            throw err;
          }
          else{
              return result
          }

Must be used with no callbacks: i.e. like this:

let result2 = await db.find(query);
         
let indexes_result = await db.index();

I don't know why they show them with those callbacks in the docs: https://github.com/cloudant/nodejs-cloudant https://www.npmjs.com/package/@cloudant/cloudant

If someone knows why they used these formulation I would be glad to know !

Upvotes: 0

Related Questions