Bluemix - Cloudant node.js:调用时出错 API
Bluemix - Cloudant node.js: Error when calling API
我有一个 node.js 应用程序可以调用 API。 API 在第一次调用时运行良好,但在第二次调用时,它 returns 此错误消息:
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);
}
});
和日志文件:
我不确定你想在这里实现什么,但它看起来很糟糕
- 您打电话给 db.list 是为了获取您所有文件的列表 - 很公平
- 然后您遍历列表中的每个文档以给出一个您从未使用过的变量'document'
- 然后,您针对列表中检索到的每个文档向 Cloudant 发出搜索请求。这些搜索请求将并行执行,因为它们是在 for 循环中启动的。所有搜索请求都是相同的,不包含有关您获取的文档的任何信息。
我猜这不是你想要做的。
您第二次收到 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
我有一个 node.js 应用程序可以调用 API。 API 在第一次调用时运行良好,但在第二次调用时,它 returns 此错误消息:
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);
}
});
和日志文件:
我不确定你想在这里实现什么,但它看起来很糟糕
- 您打电话给 db.list 是为了获取您所有文件的列表 - 很公平
- 然后您遍历列表中的每个文档以给出一个您从未使用过的变量'document'
- 然后,您针对列表中检索到的每个文档向 Cloudant 发出搜索请求。这些搜索请求将并行执行,因为它们是在 for 循环中启动的。所有搜索请求都是相同的,不包含有关您获取的文档的任何信息。
我猜这不是你想要做的。
您第二次收到 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