Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Express logs [Object: null prototype] instead of a plain object

Error[Object: null prototype] { title: 'product' }

Short answer

This is not an error. The object really has no prototype, so Node.js labels it when you log it. It comes from Node's querystring parser, which Express uses for form data when extended is false. Switch to extended: true, or check keys with Object.prototype.hasOwnProperty.call().

  • Applies to Node.js 22, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print how the object was built. A null-prototype object shows up in the logs and has no toString() or hasOwnProperty().

    node -e "const qs=require('node:querystring'); const o=qs.parse('title=product'); console.log(o, typeof o.hasOwnProperty)"
  2. 2Match what you see, then jump to its fix

    1. 1The body arrives from an HTML form and the log shows [Object: null prototype]express.urlencoded() runs with extended: falseFix
    2. 2The code imports node:querystring or url query helpers and then calls object methods on the resultYour own code calls querystring.parse()Fix

What does the error look like?

  • [Object: null prototype] { title: 'product' }
  • TypeError: obj.hasOwnProperty is not a function

Cause 1 of 2

express.urlencoded() runs with extended: false

1Check

The extended option picks the parser: querystring when false (the default), qs when true. Look at how the middleware is set up.

grep -rn "urlencoded" --include=*.js --include=*.ts .

2Fix

Set extended to true if you want the qs parser, which also supports nested objects. If flat data is enough, keep false and use safe key checks.

app.use(express.urlencoded({ extended: true }));

app.post('/products', (req, res) => {
  console.log(req.body);
  res.sendStatus(204);
});

3Verify

Post a form and read the log again.

curl -i -X POST -d 'title=product' http://localhost:3000/products

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 2

Your own code calls querystring.parse()

1Check

The object returned by querystring.parse() does not inherit from Object, so toString() and hasOwnProperty() are not defined.

grep -rn "querystring" --include=*.js --include=*.ts .

2Fix

Do not call methods on the result. Check keys through the Object prototype instead.

const qs = require('node:querystring');
const parsed = qs.parse('title=product');
if (Object.prototype.hasOwnProperty.call(parsed, 'title')) {
  console.log(parsed.title);
}

3Verify

Run the file; no TypeError should be thrown.

node parse-test.js

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Treat form data as untrusted and validate every field before use.
  • Choose the urlencoded extended setting on purpose, not by copying a snippet.
  • Use Object.prototype.hasOwnProperty.call() when you check keys on parsed query data.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.