0

I am trying to randomly print a line from a text file. I am reading the text file and return a random line from it. I passed it to the res.send() but it doesn't show up on the screen. It shows up in the log file, though. Any ideas on what is going wrong here?

This is the javascript code that I am running:

var express=require('express');
var app=express();
var port = process.env.PORT || 3000

app.get('/', function (req, res) {
  res.send(getRandomLine('listofquestions.txt'));
})
/*--------------------Routing Over----------------------------*/

function getRandomLine(filename){
var fs = require('fs');
fs.readFile(filename, function(err, data) {
    if(err) throw err;
    var array = data.toString().split("\n");
    var length = Math.floor(Math.random() * array.length) + 1 ;
        console.log(array[length]);
        return (array[length]);
});
}

app.listen(port, function () {
  console.log(`Server listening on port ${port}!`)
});

This is the file I am using to read from:

What is possible?
What if it works out exactly as you want it to?
What is the dream?
What is exciting to you about this?
What is the urge? 
What does your intuition tell you?
sheidaei
  • 9,842
  • 20
  • 63
  • 86
  • `getRandomLine` doesn't have a return statement in it, so it returns `undefined`. See the duplicate for information about how to `res.send` the `data` you get asynchronously in `fs.readFile`. – Paul Dec 09 '16 at 22:07
  • Also, side note, you probably don't want that `+ 1`, because that means you're choosing a number between `1` and `array.length` instead of `0` and `array.length - 1`, but since arrays are `0` indexed you will never choose the first line, and will get an error when you index past the end of the array sometimes. – Paul Dec 09 '16 at 22:08
  • @Paulpro when you are saying that getRandomLine doesn't have a return statement in it. Isn't return (array[length]); considered a return statement? – sheidaei Dec 10 '16 at 03:58
  • @Paulpro Also if you could help me understand what should I use it will be helpful. I looked at the linked question but got confused. I'm sure it has value to read all that but I need to learn step by step. Thanks. – sheidaei Dec 10 '16 at 04:09
  • To clarify, you have a return statement, but it is inside another function, the one that starts with `function(err, data) {`. `return` only returns from the function it's immediately in, so for `getRandomLine` to return anything the `return` statement would need to be inside `getRandomLine` directly without being inside another `function` inside that. – Paul Dec 10 '16 at 04:33
  • Basically, it's impossible to make `getRandomLine()`, return the line so instead you should make it return a Promise of a line, or accept a callback. – Paul Dec 10 '16 at 04:36
  • Thanks @Paulpro Is this something following would work: `function getRandomLine(filename){ var fs = require('fs'); fs.readFile(filename, function(err, data) { if(err) throw err; var array = data.toString().split("\n"); var length = Math.floor(Math.random() * array.length) + 1 ; console.log(array[length]); }); return (array[length]); }` – sheidaei Dec 10 '16 at 04:51
  • `function getRandomLine(filename, callback){ ... callback( array[length] ); // instead of return }` – Paul Dec 10 '16 at 18:55
  • `getRandomLine( 'listofquestions.txt', function ( line ) { res.send( line ); } );` – Paul Dec 10 '16 at 18:56
  • Thanks @Paulpro it worked. I wasn't able to add the correct form as this question is marked as duplicate. – sheidaei Dec 11 '16 at 01:14

0 Answers0