【问题标题】:Code isn't executing the full script代码没有执行完整的脚本
【发布时间】:2016-11-27 18:43:31
【问题描述】:

我编写了一些代码来检查列表,并检查列表中的每个项目是否存在于另一个中。如果未找到该项目,则将其添加到数据库中。

扫描代码是正确的(说 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

【问题讨论】:

  • 使用你的调试器来单步调试代码,看看哪里出错了。 (node-inspector 就是这样一种 NodeJS 调试器。)
  • 专业提示:代码翻转并不能说明它有多棒。快速思考这段代码是不可能的。

标签: javascript amazon-web-services express amazon


【解决方案1】:

您从未调用过函数DatabaseTime。您的代码只是声明了函数,没有做任何其他事情。为了执行该函数,您必须调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-17
    • 2020-02-23
    • 2014-07-07
    • 1970-01-01
    • 2012-01-18
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多