1

I'd like to know whether a specific string is within the other strings or not, in Javascript(node js). Here is my code.

let findFromLog = function(req, res) {
  const smsBeacon = req.body.SMS;
  const callBeacon = req.body.CALL;
  const phoneBeacon = req.body.PHONE;
  let index = 0;

  var logArray = [];

  fs.readFile('./logs/winston-test.log', 'utf8', function(err, data) {
    if (err) throw err;
    var array = data.toString().split('\n\n');

    for(var i in array) {
      if (array[i].includes(smsBeacon)) {
        logArray.push(array[i]);
      } else if (array[i].includes(callBeacon)) {
        logArray.push(array[i]);
      } else if (array[i].includes(phoneBeacon)) {
        logArray.push(array[i]);
      }
    }

//    res.json({response : array});
  });

  res.json({response : logArray});
};

the variable 'array' has these contents.

"2020-01-27T08:19:53.625Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:19:53 +0000] \"GET /opensource/uploads/beam_1_SMS.jpg HTTP/1.1\" 404 841 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36\"",
        "2020-01-27T08:19:55.404Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:19:55 +0000] \"GET /favicon.ico HTTP/1.1\" 404 2025 \"https://myserver/opensource/uploads/Object_1.jpg\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36\"",
        "2020-01-27T08:20:45.257Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:20:45 +0000] \"POST /opensource/findFromLog HTTP/1.1\" 200 609 \"-\" \"PostmanRuntime/7.22.0\"",
        "2020-01-27T08:21:23.336Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:21:23 +0000] \"POST /opensource/findFromLog HTTP/1.1\" 200 15 \"-\" \"PostmanRuntime/7.22.0\"",
        "2020-01-27T08:22:29.801Z [winston-test] info: 110.70.14.56 - - [27/Jan/2020:08:22:29 +0000] \"POST /opensource/findFromLog HTTP/1.1\" 200 29 \"-\" \"okhttp/3.12.0\"",
        "2020-01-27T08:28:11.399Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:28:11 +0000] \"POST /opensource/findFromLog HTTP/1.1\" 200 15 \"-\" \"PostmanRuntime/7.22.0\"",
        "2020-01-27T08:28:13.399Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:28:13 +0000] \"POST /opensource/findFromLog HTTP/1.1\" 200 15 \"-\" \"PostmanRuntime/7.22.0\"",
        "2020-01-27T08:31:11.957Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:31:11 +0000] \"GET /uploads/Object_2.jpg HTTP/1.1\" 404 2025 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36\"",
        "2020-01-27T08:31:21.434Z [winston-test] info: 220.94.1.121 - - [27/Jan/2020:08:31:21 +0000] \"GET /uploads/Object_3.jpg HTTP/1.1\" 404 2025 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36\"",
        "2020-01-27T08:33:23.213Z [winston-test] info: 110.70.14.56 - - [27/Jan/2020:08:33:23 +0000] \"POST /opensource/findFromLog HTTP/1.1\" 200 15 \"-\" \"okhttp/3.12.0\"",
        ""

I want to know the 'array' contains the strings a.k.a 'Object_1.jpg','Object_2.jpg', 'Object_1.jpg'. These jpg values are sent from the body (SMS, CALL and PHONE). So i used 'includes' but, it didn't work. I mean, the 'logArray' array had no value at the end of code (res.json~~).

Could you tell me what is the problem in my code?

Jun
  • 451
  • 4
  • 16
  • Note that `for-in` is not best practice for looping through arrays, see [this question's answers](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) for details. – T.J. Crowder Jan 27 '20 at 09:06
  • `if (err) throw err;` in a Node.js callback is also not best practice. – T.J. Crowder Jan 27 '20 at 09:07
  • You are looking for `Oject_1` but `array` contains `Object_1`! – Ziv Ben-Or Jan 27 '20 at 09:08
  • If your objective is to just check if either of string exists in your data, try `/Oject_.\.jpg/i.test(data.toString())` and if you need those items, `array.filter((item) => /Oject_.\.jpg/i.test(item) )` – Rajesh Jan 27 '20 at 09:10
  • `readFile` is asynchronous. When you do `res.json({response : logArray});` at the end, `logArray` is guaranteed to be empty. See the [linked question](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron)'s answers for details. Basically, move the `res.json` call *into* your callback, where the commented-out call is (but using `logArray`, not `array`, presumably). – T.J. Crowder Jan 27 '20 at 09:11
  • Oh, the oject is just a typo. Sorry ;) – Jun Jan 27 '20 at 09:14
  • 1
    @T.J.Crowder Oh, your suggestions are perfectly for me! As your suggestions, I used `for-each` instead of `for-in` and moved the `res.json` call into the readFile's callback. Then, it perfectly worked! Thank you So much. – Jun Jan 27 '20 at 09:27

0 Answers0