Ну, перед проверкой проверки данных вам нужно проверить, не является ли DB нулевым.
If Not IsDbNull(r("BedID")) Then
your code
End if
использовать async npm: https://caolan.github.io/async/docs.html
request npm: https: //www.npmjs. com / package / request
Entry.find ({}) вернет массив .... тогда мы должны пропустить каждую запись и сделать http get request .... так что мы используют модуль async npm для цикла each
, который выполняется асинхронно и делает http-вызовы.
var async=require('async');
const Entry = require('models.js');
var https = require('https');
var request = require('request');
module.exports = function getHttpsRequests () {
Entry.find({},function(err,entrys){
console.log(err);
if(!err && entrys){
async.each(entrys,function(entry,callback){
request(entry.url, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:',response.statusCode);
entry.statusCode=res.statusCode;
entry.save();
callback();
});
},function (error) {
});
}
});
}
Структурируйте свой код на несколько слоев, чтобы по крайней мере у вас было четкое представление.
Ваш DAO должен быть:
const Entry = require('models.js');
function getAll(callback){
Entry.find({}).exec(function (err, data) {
callback(err, data);
});
}
function update(id, status, callback){
Entry.findOneAndUpdate({
_id: id
}, {
$push: {statuscheck: status}
}).exec(function(err, update){
console.log(update);
callback(err, update);
});
}
Ваш цикл for должен находиться в status.js:
DAO.getAll(function(err, data){
for(var i =0; i< data.length; i++){
checkAndUpdate(data[i].url, data[i].id);
}
});
// This is seperate function to prevent "i" value to be overridden as your dependent on third party.
function checkAndUpdate(url, id){
https.get(url, function (res) {
DAO.update(id, res.statusCode, function(err, data){
console.log("updated");
});
}).on('error', function (e) {
console.error(e);
});
}