当启动app.js时,给我"无法读取未定义的属性" index.js

时间:2016-11-29 19:00:39

标签: javascript express middleware

当我尝试在我的域上节点app.js时,它给了我一个" TypeError:无法读取属性' slice'未定义"并提到index.js然而我在我的计算机上的本地虚拟主机中运行它并且它工作得很好......什么阻止了这个?我的域在ubuntu上运行,并且已经安装了脚本文件中所需的所有模块。我检查了请求的url中的源代码,它没有未定义...这很奇怪。

以下是index.js的代码:

const express = require('express');
const router = express.Router();
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const AWS = require('aws-sdk');
const ep = new AWS.Endpoint('*censored*.amazonaws.com');
const db = new AWS.DynamoDB.DocumentClient({      // Dynamo database constructor
    "apiVersion": '2012-08-10',
    "region": 'us-west-1',
    "endpoint": ep,
    "accessKeyId": '*censored*CA',
    "secretAccessKey": '+*censored*o6n'
});


var url = "https://www.google.com/search?hl=en&gl=us&tbm=nws&authuser=0&q=u.s.&oq=u.s.&num=100&start=0";

//------  START MIDDLE WARE STUFF --------------  ---------------------------------------------------------------
router.use(function timeLog (req, res, next) {
  next();
});
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});
module.exports = router; /*function(req,res,next) {
    res.render('index');
};*/
//------  END MIDDLE WARE STUFF --------------  ----------------------------------------------------------------- 





/* ------------------------------------------ ARTICLES -------------------------------------------------------- */
    // this will be called after we collect our "subject" article"
var checkArticle = function (title, date, source, url, description) {

    this.title = title;
    this.date = date;
    this.url = url;
    this.source = source;
    this.description = description;
    // set it to date inserted to database
    date = new Date().toLocaleString();

    //invoke a seperate function now! make two things process at once. 
    //goes inside the subject article and grabs the image/description
    let params = {
        TableName: "Articles"
    };

    db.scan(params, function(err,scannedData) {
        if (err) throw err;
            // scanData = target check
            // title + date + url + author = our subject article to check
            //asynchronously invoke a seperate function to display the current news right away, so there is no wait time

        console.log(`\n\n-------------------------------Subject Article:------------------------------- \n`);
        console.log("Title: " + title);
        console.log("Date: " + date);
        console.log("Source: " + source)
        console.log("description: " + description + '\n');
        console.log("URL: " + url);
            // pass this step to next function
        return Result(title, date, source, url, description, scannedData);
    });

    function Result(title, date, source, url, description,scannedData) {
        console.log("Peforming check...")
        let counter = 0;
            // peform check on all items. using 'counter' as the logic variable
        for (var i = 0; i < scannedData.Items.length; i++) {
            if (title === scannedData.Items[i].title) {
                counter++;
                console.log("We found a match. We are not performing anymore operations. \n -----------------------------------------------------------------------------");
                return console.log("Stopped from inserting article to database.");
            }
        }
            // if a  match was found, it will execute this. Adds item to database
        if (counter === 0) {
            let params = {
                TableName: "Articles",
                Item: {
                    title: title,
                    date: date,
                    url: url,
                    source: source,
                    description: description
                }
            }
            db.put(params, function(err) {
                if (err) throw err;
                else { return; }
            });
            console.log("No matches found. Inserted article to 'Articles' database");
        }
    }
}



    // This is for Front Page, news portion initiation
    request(url, 'w+', function(err,data,body) {
        if (err) throw err;
        var $ = cheerio.load(body);
        //console.log($(".slp").prev().eq(2).text());   // title
        //console.log($(".slp").prev().children().eq(0).attr('href')); // url
        //console.log($(".slp").children().html());   // New York Times - 3 hours ago      //source - date
        //console.log($("div.st").eq(0).text());    // description
        let regExp = / - /;
        console.log($(".slp").prev().children().eq(0).attr('href'));
        let urlSlice1 = $(".slp").prev().children().eq(0).attr('href').slice(7,Infinity);
        let urlSlice2 = urlSlice1.search(/&/);
        let urlSliced = urlSlice1.slice(0, urlSlice2);
        let hyphenBreakerForSource = $(".slp").children().html().search(regExp, "");        // gives the index # of found regExp
        //    got our meat of the burger.   //  /   /   //  //
        let source = $(".slp").children().html().slice(0,hyphenBreakerForSource);   //source 
        let date = $(".slp").children().html().slice(hyphenBreakerForSource + 3);       //date
        let url = urlSliced;
        let description = $("div.st").eq(0).text();
        let title = $(".slp").prev().eq(0).text();
        //          /// /   /   ////    /   /   /
        checkArticle(title, date, source, url, description);
    });

0 个答案:

没有答案