DhanaLaxshmi
DhanaLaxshmi

Reputation: 424

use of $text with mongoose for text search is not working

I am trying do text search using $text as shown below

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var port = process.env.PORT || 8000;
var router = express.Router();
var cors = require('cors');
var Schema = mongoose.Schema;
var router = express.Router();
var exec = require('exec');
app.use(bodyparser.json());

//establish the connection
var db=mongoose.connect('mongodb://localhost/book_publisherSearch');


//define the schema
var authorSchema = new mongoose.Schema(
    {
        authorId : Number,
        Description : String,
        firstName : String
    });

authorSchema.index({ firstName: 'text'});


var Authors= mongoose.model('Authors',authorSchema);

router.route('/search')
    .get(function(req,res){
        Authors.find({ $text : { $search : "Bharath" }},function(err,authors){
            console.log("inside search");
            res.send(authorss);
        })
    })


app.use(router);
app.listen(port);

but when run i am getting the error has

c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:246
        throw message;      
              ^
    TypeError: Cannot read property 'length' of undefined
        at processResults (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1581:31)
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1619:20
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1157:7
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1890:9
        at Server.Base._callHandler (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:448:41)
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:481:18
        at MongoReply.parseBody (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\responses\mongo_reply.js:68:5)
        at null.<anonymous> (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:439:20)
        at emit (events.js:107:17)
        at null.<anonymous> (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:201:13)

    Process finished with exit code 1

in the database i have a Authors collection with few collection containing the first name has Bharath

please say what i am doing wrong here thank you

Upvotes: 1

Views: 1444

Answers (1)

DhanaLaxshmi
DhanaLaxshmi

Reputation: 424

There was no documents in the collection once i added few documents it works fine

Upvotes: 2

Related Questions