2016-11-27 106 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

专业提示:代码打开它的一面是而不是它是多么棒的图表。以快速的方式思考这段代码是不可能的。 –

回答

0

你从来没有调用函数DatabaseTime。你的代码只是声明这个函数并且什么也不做。为了执行这个函数,你必须调用它。