-3

I am getting errors while using variable among two exported Node.js function

Variable name is buffer which is a Buffer.

Am i right in using variable buffer in js code ?

module.exports = {

    var buffer ;

    init: function(data) 
    {
        buffer = data ;
    },
    test: function(word) 
    {

        var readline = require('readline');
        var stream = require('stream');

        // string to buffer
        var baseText = buffer;
        var buf = new Buffer(baseText);

        //buffer to stream
        var bufferStream = new stream.PassThrough();
        bufferStream.end(buf);

        var readLines = readline.createInterface({
            input: bufferStream,
        });

        var count = 0;
        readLines.on('line', function (line)
            {
                if(line.toLowerCase().indexOf(word.toLowerCase()) != -1)
                return true;

            }
        );

        return false;
    }
}
Kevin B
  • 94,570
  • 16
  • 163
  • 180
Divyanshu Jimmy
  • 2,542
  • 5
  • 32
  • 48
  • 4
    Your syntax is invalid. You cannot declare variables inside an object literal. – SLaks Apr 27 '16 at 19:05
  • @SLaks please answer how can i correct this. This is my first Node.js code. – Divyanshu Jimmy Apr 27 '16 at 19:07
  • move it up three lines. – Kevin B Apr 27 '16 at 19:08
  • 1
    It also appears like you're trying to return a value from your function inside an async callback. Can't do that. See [How do I Return the Response From an Asynchronous Call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – jfriend00 Apr 27 '16 at 19:10

3 Answers3

0

{...} is an object literal. It can only contain property declarations (name: value).

var is a statement; you can't put it in the middle of an object literal.

You need to declare the variable separately.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

You can't declare variables in Object literals. If you define outside module.exports, then there won't any error.

var buffer ;

function init(data) {
   buffer = data ;
}

function test(word) {
    var readline = require('readline');
    var stream = require('stream');

    // string to buffer
    var baseText = buffer;
    var buf = new Buffer(baseText);

    //buffer to stream
    var bufferStream = new stream.PassThrough();
    bufferStream.end(buf);

    var readLines = readline.createInterface({
        input: bufferStream,
    });

    var count = 0;
    readLines.on('line', function (line) {
            if(line.toLowerCase().indexOf(word.toLowerCase()) != -1)
               return true;
    });
    return false;
}

module.exports = {
    init: init,
    test: test
}
Mukesh Sharma
  • 8,914
  • 8
  • 38
  • 55
0

You can't declare a variable inside an object literal.

Try something like this:

var buffer;

module.exports = {

    init: function(data) {}
    // the rest of your code

}

This is similar to the revealing module pattern.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68