0

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

4
  • What does the logs say ? Commented Nov 18, 2016 at 10:17
  • @yashpandey: I just added the log file. Please help preview. Tks Commented Nov 18, 2016 at 10:27
  • Not able to read full logs , can you take a full snapshot ? Commented Nov 18, 2016 at 10:44
  • @yashpandey: please see again. the first call API is sucessfull Commented Nov 18, 2016 at 11:01

2 Answers 2

3

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

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

1 Comment

tks so much...let me try :)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.