0
I am making a Crawler using Node.js and mongodb to save the information in the bank and I have the following code:
var request = require('request');
var cheerio = require('cheerio');
var save = require('./savedb.js');
var URL = require('url-parse');
var pageToVisit = "http://www.fateclins.edu.br/site/verNoticia.php?idNoticia=358";
var noticias = [];
console.log("Visiting page " + pageToVisit);
request(pageToVisit, function(error, response, body) {
if(error) {
console.log("Error: " + error);
}
// Check status code (200 is HTTP OK)
console.log("Status code: " + response.statusCode);
if(response.statusCode === 200) {
// Parse the document body
var $ = cheerio.load(body);
console.log("Page title: " + $('title').text());
console.log("Noticias: " + $('td').text());
}
save(noticias);
});
When I run the above code it returns me the following error:
TypeError: uri.indexOf is not a function
at checkReplicaSetInUri (C:\Users\Romulo\Documents\crawler2\node_modules\mongoose\lib\index.js:132:30)
at Mongoose.connect (C:\Users\Romulo\Documents\crawler2\node_modules\mongoose\lib\index.js:255:40)
at module.exports (C:\Users\Romulo\Documents\crawler2\savedb.js:25:13)
at Request._callback (C:\Users\Romulo\Documents\crawler2\crawler.js:22:4)
at Request.self.callback (C:\Users\Romulo\Documents\crawler2\node_modules\request\request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (C:\Users\Romulo\Documents\crawler2\node_modules\request\request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (C:\Users\Romulo\Documents\crawler2\node_modules\request\request.js:1091:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
savedb.js:
'use strict';
var mongoose = require('mongoose');
var MongoClient = require('mongodb').MongoClient;
var db = MongoClient.connect('mongodb://localhost/db');
var noticiasSchema = new mongoose.Schema({
titulo: String,
data: Date,
descricao: String
});
var noticiasModel = mongoose.model('noticias', noticiasSchema);
module.exports = function(items){
var noticias = [];
items.forEach(function(item){
var noticias = new noticiasModel();
noticias.titulo = item;
noticias.data = new Date();
noticias.descricao = item;
});
mongoose.connect(MongoClient, function(error){
if(!error){
noticiasModel.insertMany(noticias).then(function(docs){
console.log('Salvo!');
mongoose.disconnect();
}).catch(function(errot){
console.log(error);
process.exit(2);
});
}else{
console.log(error);
process.exit(1);
}
});
}
Where is the error?
Hello, Romulo, hello, Romulo! Please check the questions you asked earlier, and accept the answers that served you/helped by clicking on the checkmark next to the text. This gives a bonus to those who helped you and indicates that the problem has been solved. Thank you! :)
– Daniel
You can show this code:
savedb.js
?– Daniel
Hi Daniel, I’ll do it! about the code savedb.js I will post.
– Romulo Filho