Skip to content

Cannot read property 'statusCode' of undefined #2

@megazero1316

Description

@megazero1316

hello , i tested the node and it was working dine until i shut down the server and i forgot to turn it on, then i sent the request and found out that this error will occur and i decide to debug the problem and i found that :

if (err || !resp) {
	// node.error(RED._("httpSendMultipart.errors.no-url"), msg);
	var statusText = "Unexpected error";
	if (err) {
		statusText = err;
	} else if (!resp) {
		statusText = "No response object";
	}
	node.status({
		fill: "red",
		shape: "ring",
		text: statusText
		});
}
//no else here
msg.payload = body;
msg.statusCode = resp.statusCode || resp.status;
msg.headers = resp.headers;

if (node.ret !== "bin") {
	msg.payload = body.toString('utf8'); // txt
	if (node.ret === "obj") {
		try {
			msg.payload = JSON.parse(body);
		} catch (e) {
			node.warn(RED._("httpSendMultipart.errors.json-error"));
		}
	}
}
node.send(msg);

i fix the problem as following and also added a little bit on the code as well:

var thisReq = request.post(url, function (err, resp, body) {

    if (err || !resp || resp.statusCode == 404) {
        // node.error(RED._("httpSendMultipart.errors.no-url"), msg);
        var statusText = "Unexpected error";
        if (err) {
            statusText = err.code;
        } else if (!resp) {
            statusText = "No response object";
        }
        else if (resp.statusCode == 404) {
            statusText = resp.statusCode.toString() + " Not Found"
        }
        console.log(statusText)
        node.status({
            fill: "red",
            shape: "ring",
            text: statusText
        });
    }
    else {
        msg.payload = body;
        msg.statusCode = resp.statusCode || resp.status;
        msg.headers = resp.headers;

        if (node.ret !== "bin") {
            msg.payload = body.toString('utf8'); // txt

            if (node.ret === "obj") {
                try {
                    msg.payload = JSON.parse(body);
                } catch (e) {
                    node.warn(RED._("httpSendMultipart.errors.json-error"));
                }
            }
        }

        node.send(msg);
    }
});
var form = thisReq.form();
form.append('file', fs.createReadStream(filepath), {
    filename: filepath, // TODO: dynamically pull out just the filename
    contentType: 'multipart/form-data'
});
			}

		}); // end of on.input

node.on('close', function () {
    node.status({});
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions