我从这些 if/for 循环中取回索引 # 和“未定义”

问题描述 投票:0回答:1

所以我现在正在查看我的数据库,有 3 篇文章,所以 x 应该代表稍后 for 循环的数字 3。如果文章是新抓取的,则此代码应该将其添加到数据库中。在 Titles[] 数组中,有 100 个项目(从 news.google 中抓取)。当我执行代码时,似乎它正确地找到了标题[]列表的索引#(即55,68,60重复),但它显示为这样:(我什至不想要索引#要么显示在控制台中,我希望显示文章的标题,我不得不缩小一些代码,太长了)

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);
javascript loops for-loop web-scraping
1个回答
0
投票
  1. (打印索引): 您正在第一个

    console.log(index);
    语句中执行
    if
    ,该语句会打印数字。

  2. (未定义问题):

您正在尝试访问

titles[i]
。问题是这里既没有定义
items
也没有定义
i
。您在控制台中看到了什么?

您还尝试将

items
作为第二个参数传递给
Insert
函数,但函数声明不包含第二个参数。

如果您想打印项目的名称,您可以将更多信息传递给您的函数,如下所示:

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.");
            });
© www.soinside.com 2019 - 2024. All rights reserved.