Typeerror: Uri.indexof is not a Function

Asked

Viewed 750 times

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! :)

  • You can show this code: savedb.js?

  • Hi Daniel, I’ll do it! about the code savedb.js I will post.

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!");
    });  
}
  • Hi Daniel, what file should I include here?

  • @Romulofilho that’s it - I’m taking a look! :)

  • Daniel, I don’t understand... Should I post another part of the code? if yes which?

  • @Romulofilho no - I think you’ve posted everything that is needed!

  • Yeah, I get it, thanks for helping me out!

  • @Romulofilho please test the code I posted - I’ll take a closer look later.

  • Daniel THANK YOU VERY DEAR! It worked now, this saving in the right bank. Thank you very much!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.