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)
2
  • 1
    can you show us a more complete code example? Commented May 15, 2015 at 2:58
  • @lispHK01 i am adding entire code as update Commented May 15, 2015 at 3:01

3 Answers 3

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.

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

2 Comments

i am adding the mime variable under post @Trott
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.
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]);

1 Comment

This won't work well on global regexes where each exec() returns a different result…
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]);

4 Comments

but it still doesnt explain why if else statement works wrong
Which else statement do you mean, and how does it "work wrong"?
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
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.

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.