Nodejs:进行http请求调用并将结果更新为mongoDB

时间:2018-06-14 05:22:25

标签: node.js mongodb

我想对其他应用程序进行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();

});

0 个答案:

没有答案