Phạm Quốc Bảo
Phạm Quốc Bảo

Reputation: 894

Bluemix - Cloudant node.js: Error when calling API

I have a node.js app to call an API. The API works well on the first call, but on the second call, it returns this error message:

404 Not Found: Requested route ('abc.mybluemix.net') does not exist.

Please help review the app.js function:

app.js
    app.get('/abc/:provider_id/staffs', function(request, response) {

        console.log("Get method invoked.. ")

        db = cloudant.use(dbCredentials.dbStaff);
        //db = cloudant.use("staffs");
        var docList = [];
        db.list(function(err, body) {
            if (!err) {
                var len = body.rows.length;
                console.log('total # of docs -> '+len);
                if(len == 0) {
                    // error
                } else {
                    var i = 0;
                    body.rows.forEach(function(document) {
                        db.search('allstaff', 'allstaff_index', {q:"provider_id:"+request.params.provider_id}, function(err, doc) {
                            if (!err) {
                                if(doc['_attachments']) {
                                    // todo
                                } else {
                                    var responseDataStaff    = createResponseDataStaffs(
                                                                            doc.rows[i].fields.id,
                                                                            doc.rows[i].fields.provider_id,
                                                                                doc.rows[i].fields.firstname,
                                                                                doc.rows[i].fields.lastname,
                                                                            doc.rows[i].fields.avatar,
                                                                            doc.rows[i].fields.email,
                                                                                doc.rows[i].fields.mobile,
                                                                            doc.rows[i].fields.address,
                                                                                doc.rows[i].fields.username,
                                                                                doc.rows[i].fields.lastlogin,
                                                                            doc.rows[i].fields.lastlogout


                                                                        );
                                }
                                docList.push(responseDataStaff);
                                i++;
                                if(i >=  doc.rows.length ) {
                                    response.write(JSON.stringify(docList));
                                    console.log('ending response...');
                                    response.end();
                                }
                            } else {
                                console.log(err);
                            }
                        });

                    });

                }

            } else {
                console.log(err);
            }
        });

and log file: enter image description here

Upvotes: 0

Views: 135

Answers (2)

Avraham Shalev
Avraham Shalev

Reputation: 110

The reason you get 404 on the second time is because your app crashed. Debug it locally before you push to Bluemix.

To debug locally you need to have VCAP_SERVICES defined for your app:

Open a terminal and type cf env

Copy the content of VCAP_SERVICES to a local file (e.g. VCAP_SERVICES.json)

Create a new file next to app.js (e.g. debugApp.js) with this content

if(!process.env.VCAP_SERVICES){
 process.env.VCAP_SERVICES = JSON.stringify(require("./VCAP_Services.json"));
 }
 require('./app.js');

Then run node debugApp.js

Upvotes: 3

Glynn Bird
Glynn Bird

Reputation: 5637

I'm not sure what you're trying to achieve here but it looks bad

  1. You're calling db.list to get a list of all your documents - fair enough
  2. You then iterate through each document in the list to give a variable 'document' which you never use
  3. You then issue a search request to Cloudant for each document you retrieved in the list. These search requests will be executed in parallel because they are started in a for loop. All of the search requests are identical and do not contain anything about the document you fetched.

I'm guessing that this isn't what you intended to do.

Upvotes: 2

Related Questions