I'm doing a crawler using node.js and mongodb to save the information in the database 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?