在Node.js中,调用另一个JS文件中定义的函数的循环正在出错

时间:2022-06-06 16:03:01

I have a server (made with Express in Node.js) that gets notifications of RSS feeds, gets data from their entries (title, date, link) and then "does something" with the data by calling a function defined in another JS file ("article_filter_toDB.js"). The code on the server-side is:

我有一个服务器(用Node.js中的Express制作),它获取RSS提要的通知,从他们的条目(标题,日期,链接)获取数据,然后通过调用另一个JS文件中定义的函数对数据做“做某事” ( “article_filter_toDB.js”)。服务器端的代码是:

// parts omitted
var article_filter_toDB = require('./article_filter_toDB.js');

// parts omitted

client.on('notification', function (notification) {
    // gets notifications of RSS feeds

    entries = notification.entries;
    for (index = 0; index < entries.length; ++index) {
        title = entries[index].title;
        date = entries[index].published;
        link = entries[index].link.href;
        // gets data from the entry of the feed
        miniwords = 1000;
        // a variable that I set

        article_filter_toDB(link, title, miniwords);
        // "does something" by calling a function defined in another JS file ("article_filter_toDB.js")
    }
});

// parts omitted

What the function "article_filter_toDB" does is to get the content of the article given by the link from the RSS feed (using Request), parsing HTML code to count the words of the article, and, if this length is above "miniwords" (here 1000), save the data relative to the article (title, link, date...) to a database (MongoDB, via Mongoose).

“article_filter_toDB”函数的作用是从RSS提要(使用Request)获取链接给出的文章内容,解析HTML代码以计算文章的单词,如果此长度高于“miniwords”(这里1000),将相对于文章(标题,链接,日期......)的数据保存到数据库(MongoDB,通过Mongoose)。

Sometimes it works well. But sometimes it computes a length equal to 1 (that is, it was unable to really count the words) although, if I run the function "article_filter_toDB" separately (that is, the separate JS file, applied to same "link", "title", "miniwords" that I copy to it), it is able to correctly count the words.

有时它运作良好。但有时它计算的长度等于1(也就是说,它无法真正计算单词),但是,如果我单独运行“article_filter_toDB”函数(即单独的JS文件,应用于相同的“链接”,“标题“,”迷你词“我复制到它”,它能够正确计算单词。

Do you know what I'm doing wrong? Thanks!

你知道我做错了什么吗?谢谢!

To be more complete, here is the code of the "article_filter_toDB.js" file:

为了更完整,这里是“article_filter_toDB.js”文件的代码:

// parts omitted
article_filter_toDB = function (link, title, miniwords) {

    Article.findOne({
        title: title
    }, 'title', function (err, articles) {
        if (err) return console.error(err);

        if (articles == null) {
            // ...if an article with this title is not already present in my database...

            // parts omitted here, that set the variable "balise" depending on the link

            request(link, function (err, resp, body) {
                $ = cheerio.load(body);
                texte = $(balise).text();
                content = texte.split(" ");
                length = content.length;
                // ...let's count its words with Request and Cheerio...

                if ((length > miniwords)) {
                    var newArticle = new Article({
                        site: url.parse(link).hostname.replace(/^www\./, ''),
                        date: date,
                        link: link,
                        title: title,
                        length: length,
                    });
                    newArticle.save(function (err, newArticle) {
                        if (err) return console.error(err)
                    });
                    // if the article's length is more than the number given by "miniwords", let's save its data in my database

                }
            });
        }
    });
}

module.exports = article_filter_toDB;
// exportation of the function to use it elsewhere

1 个解决方案

#1


1  

This how you call functions from another file properly in node.js

这是如何在node.js中正确调用其他文件中的函数

// otherfile.js
// ========
module.exports = {
  article_filter_toDB: function (link, title, miniwords) {
    // do stuff here
  },
};

Then on your code:

然后在你的代码上:

var otherfile = require('./otherfile');
...
otherfile.article_filter_toDB(link, title, miniwords);   

#1


1  

This how you call functions from another file properly in node.js

这是如何在node.js中正确调用其他文件中的函数

// otherfile.js
// ========
module.exports = {
  article_filter_toDB: function (link, title, miniwords) {
    // do stuff here
  },
};

Then on your code:

然后在你的代码上:

var otherfile = require('./otherfile');
...
otherfile.article_filter_toDB(link, title, miniwords);