TypeError: uri.indexOf is not a function

0

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?

    
asked by anonymous 01.06.2017 / 01:08

1 answer

1
  

I need to take a closer look at the code, but test this change:

var request = require('request');
var cheerio = require('cheerio');
var save = require('./savedb.js');
var URL = require('url-parse');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/noticias');
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);
        noticias.push("Page title:  " + $('title').text());
    }
    save(noticias);
});
  

savedb.js

'use strict';
var mongoose = require('mongoose');
var noticiasSchema = new mongoose.Schema({
    titulo: String,
    data: Date,
    descricao: String
});

var noticiasModel = mongoose.model('noticias', noticiasSchema);
module.exports = function(items){
    var noticias = new noticiasModel();

    items.forEach(function(item){
        noticias.titulo = item;
        noticias.data = new Date();
        noticias.descricao = item;
    });

    noticias.save(function (err, Noticia) {
        if (err) return console.error(err);
            console.log("salvo!");
    });  
}
    
01.06.2017 / 01:35