我想对其他应用程序进行REST API调用,并使用响应更新现有的mongoDB集合。
我能够分开做两件事,但是当我把两者都结合起来时它不起作用。知道我错过了什么吗?
var MongoClient = require('mongodb').MongoClient;
const args = process.argv;
const request = require('request');
var today = process.argv[2];
var url = "<mongodburl>"
MongoClient.connect(url, function(err, db) {
if(err) { console.log(err); }
console.log("Connected to the DB Successfully");
const options = {
hostname: 'myapp.com',
path: '/17/describe',
method: 'GET',
headers: {
'Authorization': 'Basic c3ZjLm5naS5jZLTFhv'
}
};
callback = function(response) {
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
callbackData(str);
});
}
var req = http.request(options, callback).end();
var cursor = db.collection("info").find({ 'day' : today });
cursor.each(function(err,doc){
if (doc != null){
db.collection("cd_containers_info").findAndModify({"pod_name":
"j7c0k9"},[['_id','asc']],{$set :{"status":str}},{w:1},function(err,
result) {
if (err) throw err;
});
}
});
db.close();
});