【问题标题】:Bluemix - Cloudant node.js: Error when calling APIBluemix - Cloudant node.js:调用 API 时出错
【发布时间】:2016-11-18 09:44:36
【问题描述】:

我有一个 node.js 应用程序来调用 API。 API 在第一次调用时运行良好,但在第二次调用时,它返回以下错误消息:

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

请帮助审核 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);
            }
        });

和日志文件:

【问题讨论】:

  • 日志说什么?
  • @yashpandey:我刚刚添加了日志文件。请帮忙预览。 Tks
  • 无法读取完整的日志,你能拍个完整的快照吗?
  • @yashpandey:请再看一遍。第一次调用API成功

标签: javascript node.js ibm-cloud cloudant


【解决方案1】:

您第二次收到 404 的原因是您的应用崩溃了。 在推送到 Bluemix 之前在本地对其进行调试。

要在本地调试,您需要为您的应用定义 VCAP_SERVICES:

打开终端并输入 cf env

将 VCAP_SERVICES 的内容复制到本地文件(例如 VCAP_SERVICES.json)

在 app.js 旁边创建一个包含此内容的新文件(例如 debugApp.js)

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

然后运行node debugApp.js

【讨论】:

  • tks 这么多...让我试试 :)
【解决方案2】:

我不确定你想在这里实现什么,但看起来很糟糕

  1. 您正在调用 db.list 以获取所有文档的列表 - 很公平
  2. 然后您遍历列表中的每个文档以提供一个您从不使用的变量“文档”
  3. 然后,您针对您在列表中检索到的每个文档向 Cloudant 发出搜索请求。这些搜索请求将并行执行,因为它们是在 for 循环中启动的。所有搜索请求都是相同的,并且不包含有关您获取的文档的任何内容。

我猜这不是你打算做的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多