我有一个在本地运行良好的应用程序,但一旦在heroku上,它就无法正常工作。我的应用程序具有scrape功能,可将检索到的文章从网站存储到数据库中。问题是,由于某些原因,这对Heroku不起作用,我收到错误MongoError: Invalid Operation, No operations in bulk
。我尝试使用new Promise
和ORM
,但我仍然遇到同样的错误。奇怪的是,当我使用new Promise
时,这在heroku上运行了一次,但在我去了一条名为/delete
的路线之后,由于某种原因,刮痕停止了工作。此路由只删除我的两个模型中的所有数据。我现在不确定问题是什么。可能是因为当我在insertMany
模型中执行Article
时,有些空属性未被分配?
更新:我只是在scrappedStuff
函数中放置了数组scraper
的console.log,它显示了heroku日志中的数组为空。为什么显示数组是空的?在.each()
完成后,是否应该调用传递数组的回调函数?
//我的模特
var ArticleSchema = new Schema({
title: {
type: String
},
link: {
type: String
},
description: {
type: String
},
saved: {
type: Boolean,
default: false
},
img: {
type: String
},
comment: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}]
});
// ROUTE FILE
var scrape = require('./scrape');
var scraper = function (response, scrapedStuff, displayScrape) {
console.log("INSIDE SCRAPER CALLBACK FUNCTION (INSERTMANY)", scrapedStuff)
console.log("TWOOOOOOOOOOOOOOOOOOOOOOOOOO")
Article.collection.insertMany(scrapedStuff, { ordered: false }, function(err, docs){
if(err){
console.log(err);
}else{
console.log("INSERT MANY DOCS", docs);
console.log("FINISHED SCRAPING ELSE")
// resolve();
displayScrape(response);
}
});
}
var displayScrape = function(response){
console.log("THREEEEEEEEEEEEEEEEEEEEEEE")
Article.find({}, function(err, doc){
if(err){
console.log(err)
}else{
if(doc.length > 0){
console.log("CHECK TO SEE IF DOCS IN HOME GOT UPDATE", doc)
var articleLength = [];
for(var x = 0; x < doc.length; x++){
if(doc[x].saved === false){
articleLength.push(doc[x]);
}
}
// var finalLength = articleLength.length;
response.render('home', {data: {articles: doc, length: articleLength.length, finishScrape: true}})
}else{
response.render('home');
}
}
});
}
router.get('/scrape', function(req, response){
scrape(response, scraper, displayScrape);
});
// SCRAPE FILE
var request = require('request');
var cheerio = require('cheerio');
var scrape = function(response, scraper, displayScrape) {
console.log("ONNNNNNNNNEEEEEEEEEEE")
request('http://www.gamespot.com/', function(err, res, html){
if(err)
{
console.log(err)
// reject(err);
}
// else
var scrapedStuff = [];
console.log("GOT RESPONSE FROM GAMESPOT");
var $ = cheerio.load(html);
// var articleCounter = 1;
$('article a').each(function(i, element){
// var scrapedStuff = {};
// scrapedStuff.articleID = articleCounter;
var scrpObj = {
title: $(this).attr('data-event-title'),
link: $(this).attr('href'),
img: $(this).children('figure').children('div.media-img').children('img').attr('src'),
description: $(this).children('div.media-body').children('p.media-deck').text()
}
scrapedStuff.push(scrpObj);
});
console.log("SCRAPED RESULTS", scrapedStuff);
scraper(response, scrapedStuff, displayScrape);
});
}
module.exports = scrape;