Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Node.js脚本没有';t出口_Javascript_Node.js_Promise - Fatal编程技术网

Javascript 为什么Node.js脚本没有';t出口

Javascript 为什么Node.js脚本没有';t出口,javascript,node.js,promise,Javascript,Node.js,Promise,我编写了从HTTP服务获取数据并将其保存在MongoDB中的代码。 现在我直接从控制台node script.js 脚本完成了它的工作,但没有退出。我必须按CTRL+C组合键才能停止它 为什么它不会自动退出?代码中的哪个位置最适合流程。退出(0)(如果需要) /*全球承诺、getData、httpReq、searchData、getHotData、storeData*/ var http=require('http'); var CoubVideo=require('./models/coub

我编写了从HTTP服务获取数据并将其保存在MongoDB中的代码。
现在我直接从控制台
node script.js

脚本完成了它的工作,但没有退出。我必须按CTRL+C组合键才能停止它

为什么它不会自动退出?代码中的哪个位置最适合
流程。退出(0)
(如果需要)

/*全球承诺、getData、httpReq、searchData、getHotData、storeData*/
var http=require('http');
var CoubVideo=require('./models/coubdb.js');
函数CoubApi(url、numPerPage、numOfPages){
this.url=url;
this.numopages=numopages;
this.numPerPage=numPerPage;
this.getData=getData;
this.httpReq=httpReq;
this.searchData=searchData;
this.getHotData=getHotData;
this.storeData=storeData;
}
//从服务器获取数据
函数httpReq(url){
var承诺=新承诺(功能(解决、拒绝){
get(url,函数(res){
var数据=“”;
res.on('data',函数(块){
数据+=块;
});
res.on('end',function(){
如果(data.length>0){
解析(JSON.parse(data));
}否则{
拒绝(“错误:HTTP请求被拒绝!”);
}
});
}).on('error',函数(err){
控制台日志(err);
});
});
回报承诺;
}
//在MongoDB中存储数据
函数存储数据(数据,每页){
函数insertVideoDoc(i){
CoubVideo.count({id:data.coubs[i].id},函数(err,count){
如果(错误){
控制台日志(err);
}
如果(计数===0 | |计数===null){
var video=新CoubVideo(data.coubs[i]);
视频保存(功能(错误){
如果(错误){
控制台日志(err);
}
log(“保存成功!”);
});
}否则{
控制台日志(“副本”);
}
});
}
var i;
对于(i=0;i
处理完与数据库的连接后,是否要关闭连接?打开的连接将保持节点实例运行,因此,最后我必须执行
mongoose.connection.close()
,对吗?@PatrickEvans现在我不会在代码的任何部分关闭连接。处理完这些连接后,是否关闭与数据库的连接?打开连接将使节点实例保持运行,因此,最后我必须执行
mongoose.connection.close()
,对吗?@PatrickEvans现在我不会在代码的任何部分关闭连接。
/*global Promise, getData, httpReq, searchData, getHotData, storeData*/

var http = require('http');
var CoubVideo = require('./models/coubdb.js');

function CoubApi (url, numPerPage, numOfPages) {
  this.url = url;
  this.numOfPages = numOfPages;
  this.numPerPage = numPerPage;
  this.getData = getData;
  this.httpReq = httpReq;
  this.searchData = searchData;
  this.getHotData = getHotData;
  this.storeData = storeData;
}

// Get data from server
function httpReq (url) {
  var promise = new Promise (function (resolve, reject) {

    http.get(url, function (res) {
      var data = '';

      res.on('data', function (chunk) {
        data += chunk;
      });

      res.on('end', function () {
        if(data.length > 0) {
          resolve(JSON.parse(data));
        } else {
          reject("Error: HTTP request rejected!");
        }
      });

    }).on('error', function (err) {
      console.log(err);
    });

  });

  return promise;
}

// Store data in MongoDB
function storeData (data, per_page) {

  function insertVideoDoc (i) {
    CoubVideo.count({id: data.coubs[i].id}, function (err, count) {
      if (err) {
        console.log(err);
      }

      if (count === 0 || count === null) {
        var video = new CoubVideo(data.coubs[i]);

        video.save(function (err) {
          if (err) {
            console.log(err);
          }
          console.log("Saved successfully!");
        });
      } else { 
        console.log("Duplicate");
      }
    });
  }

  var i;
  for(i = 0; i < per_page; i++) {
    insertVideoDoc(i);
  }
}

// Search for coubs
function searchData (searchtext, order, page, per_page) {
  var url = this.url +
            "search?q=" + searchtext +
            "&order_by=" + order +
            "&per_page=" + per_page + 
            "&page=" + page;

  this.httpReq(url).then(function (data) {
    this.storeData(data, per_page);
  }.bind(this));
}

// Get hot coubs
function getHotData (order, page, per_page) {
  var url = this.url +
            "timeline/hot?page=" + page +
            "&per_page=" + per_page + 
            "&order_by=" + order;

  this.httpReq(url).then(function (data) {
    this.storeData(data, per_page);
  }.bind(this));
}

// Get data
function getData () { 
  var i;
  for(i = 0; i < this.numOfPages; i++) {
    this.getHotData("newest_popular", i, this.numPerPage);
  }
}

var coub = new CoubApi("http://coub.com/api/v2/", 2, 50);
coub.getData();