0

I am trying to add some html tags and list as a string to var in javascript. So that I can replace later that code in html.

app.use('/index', function(req, res) {
  var query = req.query;
  var searchQuery = query.search;
  for(i = 0; i < 11; i++){
    if (data[i].indexOf(searchQuery) != -1){
      console.log(data[i]);
      //document.getElementById("".innerHTML = )
     newdata.concat("<li>",data[i],"<li><br>");
      console.log(newdata);
    }
  };
  console.log(newdata);
  var fileContent = fs.readFileSync(path.join(__dirname, '/public/index.html'), "utf8");
  res.send(fileContent.replace("{{data}}", newdata));
});

I am trying to add info to newdata but concat doens't seem to work. Can you please help me and tell me why? I print data before adding to newdata with concat, but after additon newdata is still empty. WHY?

Just in case whole code is here

var express = require('express');
var path = require('path');
var app = express();
var port = process.env.PORT || 3000;
var fs = require ('fs')
var data = ['yellow','mellow','hello','yellow jaws','jaws','jaws 2','jaws 3','mellow jaws','angry jaws','angry jews','angry yellow juice'];
var newdata = '';
app.use(express.static(path.join(__dirname, '/public')));

app.use('/index', function(req, res) {
  var query = req.query;
  var searchQuery = query.search;
  for(i = 0; i < 11; i++){
    if (data[i].indexOf(searchQuery) != -1){
      console.log("VATO");
      console.log(data[i]);
      //document.getElementById("".innerHTML = )
     newdata.concat("<li>",data[i],"<li><br>");
      console.log(newdata);
    }
  };
  console.log(newdata);
  var fileContent = fs.readFileSync(path.join(__dirname, '/public/index.html'), "utf8");
  res.send(fileContent.replace("{{data}}", newdata));
});

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname, '/public/index.html'));
});

app.listen(port, function() {
  console.log('App is listening on port ' + port);
});
3
  • Wasnt this question posted 2 hours ago? Commented Nov 17, 2015 at 3:49
  • I took down that one and did this one instead. cause that wasn't describing the main problem. Commented Nov 17, 2015 at 3:50
  • @Oleander did u -1 the question? Commented Nov 17, 2015 at 3:57

1 Answer 1

1

Concat does not modify the original string. It returns the modification. Change this:

newdata.concat("<li>",data[i],"<li><br>");

to

newdata = newdata.concat("<li>",data[i],"</li><br>");

(I also closed the li tag)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.