简体   繁体   English

Bluemix-Cloudant node.js:调用API时出错

[英]Bluemix - Cloudant node.js: Error when calling API

I have a node.js app to call an API. 我有一个node.js应用程序来调用API。 The API works well on the first call, but on the second call, it returns this error message: 该API在第一个调用中运行良好,但是在第二个调用中,它返回以下错误消息:

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

Please help review the app.js function: 请帮助查看app.js函数:

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: 和日志文件: 在此处输入图片说明

The reason you get 404 on the second time is because your app crashed. 您第二次收到404的原因是因为您的应用程序崩溃了。 Debug it locally before you push to Bluemix. 在推送到Bluemix之前,先在本地对其进行调试。

To debug locally you need to have VCAP_SERVICES defined for your app: 要在本地调试,您需要为您的应用定义VCAP_SERVICES:

Open a terminal and type cf env 打开一个终端并输入cf env

Copy the content of VCAP_SERVICES to a local file (eg VCAP_SERVICES.json) 将VCAP_SERVICES的内容复制到本地文件(例如VCAP_SERVICES.json)

Create a new file next to app.js (eg debugApp.js) with this content 使用以下内容在app.js(例如debugApp.js)旁边创建一个新文件

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

Then run node debugApp.js 然后运行node debugApp.js

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 您正在调用db.list以获得所有文档的列表-足够公平
  2. You then iterate through each document in the list to give a variable 'document' which you never use 然后,您遍历列表中的每个文档以提供一个从未使用过的变量“ document”
  3. You then issue a search request to Cloudant for each document you retrieved in the list. 然后,您针对列表中检索到的每个文档向Cloudant发出搜索请求。 These search requests will be executed in parallel because they are started in a for loop. 这些搜索请求将在for循环中启动,因此将并行执行。 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. 我猜这不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM