Stock lookups should check own properties to avoid inherited SKU false matches

A JSON stock record keyed by SKU can return a false match for a name like 'constructor', because the in operator also checks inherited properties from Object.prototype. The article recommends using Object.hasOwn() to confirm the record itself contains the requested key before reading its quantity. It presents a small Node.js helper that validates the input, returns a distinct missing result for absent SKUs, validates quantities, and leaves the input record unchanged. The author notes this applies only to parsed JSON stock objects, not arbitrary live objects or full warehouse responses. The example is a read-only lookup and does not describe a deployed or tested store integration.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in