2016-11-28 10 views
0

私は現在データベースを探していますが、3つの記事がありますので、xは後でforループの番号3を表す必要があります。このコードは、新しくスクラップされた記事をデータベースに追加することになっています。 titles []配列には100個のアイテムがあります(news.googleから抜粋)。私はコードを実行すると、title []リストのインデックス番号(55,68,60リピート)を正しく見つけるようですが、次のように表示されます。(インデックス# 。これらのif/forループからindex#と "undefined"を取得しています

)私はあまりにも長い間、このコードの一部を縮小しなければならなかった私は、記事のタイトルを表示させたい、どちらかのコンソールで
55 
68 
60 
55 
68 
60 
55 
68 
60 
55 
68 
60 
55 
68 
60 
55 
68 
Complete. 
Successfully added article: undefinedto the database. 
Successfully added article: undefinedto the database. 
Successfully added article: undefinedto the database. 
Successfully added article: undefinedto the database. 
Successfully added article: undefinedto the database. 
Successfully added article: undefinedto the database. 

を表示し、ここに私のコードですさ:

// accessing the database 
function DatabaseTime(sourcesDates, timeAdded, links, titles, descriptions) { 
    sourcesDates = sourcesDates; 
    links = links; 
    titles = titles; 
    descriptions = descriptions; 


    // put counter so params can access this in it's object scope, use it for the for-loop 
    // object operator. MEAT OF THE BURGER 
    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 
      //use this array later to hold the unique items 
      var arrayCheck = []; 
      for (let i = 0; i < scanData.Items.length; i++) { 
       arrayCheck.push(scanData.Items[i].title); 
      } 
      var index = 0; 
      var counter = 0; 
      var x; 


      for (var i = 0; i < titles.length; i++) { 
        index = 0; 
        x = 0; 
       for (var x = 0; x < arrayCheck.length; x++) { 

        //if (titles[i] === arrayCheck[x]) { 
        if (titles.indexOf(arrayCheck[x] === -1)) { 
         index = titles.indexOf(arrayCheck[x]); 
         console.log(index); 
         var autoParams = { 
          TableName: "Rnews", 
          Item: { 
           title: titles[index], 
           source: sourcesDates[index], 
           url: links[index], 
           description: descriptions[index], 
           lastAddOrUpdated: dbTimeStamp, 
           timePublish: timeAdded[index] 
          } 
         } 
         Insert(autoParams, titles); 
        } 
        } 
       } 
      function Insert(autoParams) { 
       db.put(autoParams, function(err, data) { 
        if (err) throw err; 
        console.log("Successfully added article: " + titles[i] + "to the database."); 
       }); 
      } 
      console.log("Complete."); 
     }); 
    }; 
    databaseOperation(sourcesDates, timeAdded, links, titles, descriptions); 
} 
//// END 

DatabaseTime(sourcesDates, timeAdded, links, titles, descriptions); 

答えて

0
  1. (インデックスを印刷する): console.log(index);を最初のifステートメントで実行しています。このステートメントは、数値rs。

  2. (未定義の問題):あなたがtitles[i]にアクセスしようとしている

。問題はここではitemsでもiも定義されていないということです。あなたのコンソールには何が表示されますか?

また、itemsを第2引数としてInsert関数に渡そうとしていますが、関数宣言には第2引数が含まれていません。

アイテムの名前を印刷したい場合、あなたはこのようなあなたの機能に多くの情報を渡すことができ、次のいずれか

function Insert(autoParams, currentItemsName) { 
      db.put(autoParams, function(err, data) { 
       if (err) throw err; 
       console.log("Successfully added article: " + currentItemsName + " to the database."); 
      }); 

し、それを呼び出す:

Insert(autoParams, titles[i]); 

の代わり:

Insert(autoParams, titles); 

また、関数内ですでに利用可能なデータを使用して、それを印刷することもできます。私の意見では、より良い選択肢です。

function Insert(autoParams) { 
      var currentItemsName = autoParams.Item.title 
      db.put(autoParams, function(err, data) { 
       if (err) throw err; 
       console.log("Successfully added article: " + currentItemsName + " to the database."); 
      }); 
関連する問題