0

I have a buffer in node.js and I'm checking for mime type with regex.
There is a capturing group in regex and if it is successfull it must return this capturing group at index 1 in the array returned by exec.

I'm using

if(mime.exec(dt)[1]){
    tip.push(mime.exec(dt)[1]);
}

this control and I also tried

if(1 in mime.exec)

and also

mime.exec.hasOwnProperty(1)

but anyway the condition is processed and gives traceback

TypeError: Cannot  read property '1' of null

What kind of mechanism can I use to fix this issue?

UPDATE ----

var mime=/^content-type: (.+\S)/igm;

UPDATE ----

var fs = require("fs"),
    mime = /^content-type: (.+\S)/igm,
    tip = [];
require("http").createServer(function(req, res) {
    var data = "";
    console.log("working...");
    console.log(req.method);
    if (req.method.toUpperCase() == "POST") {

        req.once("data", function() {
            fs.writeFileSync("dene.txt", "");
        });
        req.on("data", function(dt) {
            fs.appendFileSync("dene.txt", dt.toString("utf8"));
            if (mime.exec(dt)[1]) {
                tip.push(mime.exec(dt)[1]);
            } else {
                return false;
            }

        });

        req.on("end", function() {
            console.log(((fs.statSync("dene.txt").size) / 1024).toFixed(2), "kb");
            console.log(tip);

        });
    }
    res.writeHead(200, {
        "content-type": "text/html"
    });
    res.end(require("fs").readFileSync(require("path").resolve(__dirname, "static_files/post.html")));
}).listen(3000)
nikoss
  • 3,254
  • 2
  • 26
  • 40

3 Answers3

2

Without more context (especially how is the value of mime assigned), it is difficult to say exactly what is going on, but what we can say with certainty is: mime.exec is null at the time that your code executes mime.exec.hasOwnProperty(1). So fire up a debugger and watch the value of mime to see what's going on.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • i am adding the mime variable under post @Trott – nikoss May 15 '15 at 02:51
  • Somewhere between that assignment and where you are checking for the property, the value of `mime` is changed. It might be a case of using `=` where you mean to use `==`. It might be a scope issue. It might be something else. – Trott May 15 '15 at 02:58
1

The problem is that your regex has the global flag set - compare Why RegExp with global flag in Javascript give wrong results?. So when you call mime.exec(dt) the first time, it matches something and advances the mime.lastIndex property, but when you call mime.exec(dt) a second time it doesn't find a second match in the dt string.

So there are two things to do:

  • Don't make it a global regex when you only intend to make a single match.
    Alternaticely, if you plan to reuse the object (like the multiple callback invocations in your example), make sure to either exhaust the search (typically while (m = regex.exec(input))) or reset regex.lastIndex=0; every time.
  • Don't call exec() twice, but just store the result in a variable

Also notice that .exec() might not return an array at all but null when it doesn't match anything, so you'll anyway have to use

var match = mime.exec(dt);
if (match) // possibly `&& match[1]` if you need to ensure that no empty string was captured
    tip.push(match[1]);
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • but it still doesnt explain why if else statement works wrong – nikoss May 15 '15 at 03:06
  • Which else statement do you mean, and how does it "*work wrong*"? – Bergi May 15 '15 at 03:12
  • im checking if first index of the array whic is represented by exec defined and then making this push but anyway even if this is null the code in if block executes – nikoss May 15 '15 at 03:14
  • Are you saying that `tip` contains unexpected values in the end? If so, please post a test case with the values of `dt` you are receiving and the expected output. Or are you refering to the `Cannot read property '1' of null` type error? See the last paragraph of my answer. – Bergi May 15 '15 at 03:21
1

change this

if (mime.exec(dt)[1]) {

to this

if (mime.exec(dt) && mime.exec(dt)[1]) {

exec returns either null or an array -- test for null first because you can't treat null as an array.

Edit: as mentioned in the comments, there probably will be additional considerations to keep in mind if using a global regex.

So, for global regexes, super-safe version:

var rslt = mime.exec(dt)
if (rslt && rslt[1]) {
  tip.push(rslt[1]);
user2524973
  • 1,087
  • 6
  • 14
  • 1
    This won't work well on global regexes where each `exec()` returns a different result… – Bergi May 15 '15 at 03:22