2016-11-27 4 views
0

Я написал код, который проверяет список и проверяет, присутствует ли каждый элемент в списке в другом. Если элемент не найден, он добавляет его в базу данных.Код не исполняет полный скрипт

Код сканирования правильный (часть, которая говорит db.scan), но где-то ближе к концу код не проходит, потому что он не выполняет часть console.log (где он говорит «Ввод журнала в базу данных ..» . «название статьи» Когда я выполняю этот код, ничего не происходит. по крайней мере, нет никаких ошибок ... но его даже не регистрируя console.log части так что-то не так.

// accessing the database 
function DatabaseTime(sourcesDates, timeAdded, links, titles, descriptions) { 
    sourcesDates = sourcesDates; 
    links = links; 
    titles = titles;  // this will be used to check on our articles 
    descriptions = descriptions; 

    var autoParams; 
    var databaseOperation = function (sourcesDates, timeAdded, links, titles, descriptions) { 
     var scanParams = { TableName: "Rnews" } 
      // using code to setup for accessing the 2nd list 
      db.scan(scanParams, function(err, scanData) { // scanData = the 2nd list we are going to work with 
       var counter = 0; // just a way to help make my code more accurate as seen later in the loops 
       var counter2 = 0; 
       // this is the first list iterating on 
       for (var i = 0; i < links.length; i++) { 
        counter = 0; 
        // looping through items in second list 
        for (var x = 0; x < scanData.Items.length; x++) { 
         // if article is not in db 
         if (titles[i] !== scanData.Items[x].title) { 
          continue; 
         } 
         else if (titles[i] === scanData.Items[x].title) { 
          // intention is to immediately move to the next item in the first list if this block executes 
          console.log("Article found: \"" + titles[i] + "\". Not proceeding anymore with article."); 
          counter++; 
          break; 
         } else { 
          // if this article isnt found anywhere in the list we are checking on, add to database 
          if (x === scanData.Items.length && counter !== 0) { 
           autoParams = { 
            TableName: "Rnews", 
            Item: { 
             title: titles[i], 
             source: sourcesDates[i], 
             url: links[i], 
             description: descriptions[i], 
             lastAddOrUpdated: dbTimeStamp, 
             timePublish: timeAdded[i] 
            } 
           } 
           console.log("Entering journal to database: " + titles[i]); 
           db.put(autoParams, function(err, data) { 
            if(err) throw err; 
           }); 
          //} 
         } 
        } 
       } 
      } 
      }); 
     //console.log("Complete"); 
    }; 
    databaseOperation(sourcesDates, timeAdded, links, titles, descriptions); 
} 
//// END 
+1

Используйте свой отладчик, чтобы выполнить код и посмотреть, где он не работает. (['node-inspector'] (https://www.npmjs.com/package/node-inspector) является одним из таких отладчиков для NodeJS.) –

+0

Спасибо, я попробую, чтобы – Chris

+0

Pro tip: код, включенный на его стороне, не график того, насколько это удивительно. Невозможно быстро подумать об этом коде. –

ответ

0

Вы никогда не вызывал функцию DatabaseTime. Ваш код просто объявляет функцию и ничего не делает. Для того, чтобы функция выполнялась, вы должны ее вызывать.