Finding values in an Array in Javascript

I'm trying to find out if a value exists in an array. The following code is giving me an error each time I run saying Object has no replace method.

var fruits = ['apples', 'pears', 'bananas'];

console.log("Enter in a fruit name");

process.stdin.on('data', function(fruit) {

    fruit = fruit.replace("\n", "");
    if (fruits.indexOf(fruit) >= 0 ) {
        console.log("The value has been found in the array");
        process.exit(); }

    else {
        console.log("Value not found");
        process.exit(); }

});

At first it kept returning "Value not found" no matter what I entered, so I surmised it was the line break/enter that I press after entering my fruit. But the replace method for the fruit refuses to take. What am I missing?

If you haven't used the setEncoding method, the data event gets a Buffer object, not a string.

Use the toString method to decode the data in the buffer to a string:

var fruitName = fruit.toString().replace("\n", "");

It's possible that the reason that you didn't find anything in the array is that you were looking for the Buffer object instead of a string. In that case you might not need the replace after all.