Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Stream writable.writableObjectMode Property in Node.js
The writable.writableObjectMode property is used to check whether a writable stream is operating in object mode. It returns true if object mode is enabled, false if disabled, or undefined if not explicitly set.
Syntax
writable.writableObjectMode
Return Value
Returns a boolean value or undefined:
-
true- Object mode is enabled -
false- Object mode is explicitly disabled -
undefined- Object mode not set (default)
Example 1: Stream with Object Mode Enabled
// Program to demonstrate writable.writableObjectMode property
// Importing the stream module
const stream = require('stream');
// Creating a data stream with writable and objectMode set to true
const writable = new stream.Writable({
objectMode: true,
// Writing the data from stream
write: function(chunk, encoding, next) {
// Converting the data chunk to be displayed
console.log(chunk.toString());
next();
}
});
// Writing data
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING');
// Checking if object mode is enabled
console.log('Object mode enabled:', writable.writableObjectMode);
Welcome to TutorialsPoint ! SIMPLY LEARNING Object mode enabled: true
Example 2: Stream with Default Settings
// Program to demonstrate writable.writableObjectMode property
// Importing the stream module
const stream = require('stream');
// Creating a data stream with writable (default settings)
const writable = new stream.Writable({
// Writing the data from stream
write: function(chunk, encoding, next) {
// Converting the data chunk to be displayed
console.log(chunk.toString());
next();
}
});
// Writing data
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING');
// Checking object mode status
console.log('Object mode status:', writable.writableObjectMode);
console.log('Type:', typeof writable.writableObjectMode);
Welcome to TutorialsPoint ! SIMPLY LEARNING Object mode status: undefined Type: undefined
Example 3: Stream with Object Mode Explicitly Disabled
// Creating a writable stream with objectMode explicitly set to false
const stream = require('stream');
const writable = new stream.Writable({
objectMode: false,
write: function(chunk, encoding, next) {
console.log('Received:', chunk.toString());
next();
}
});
writable.write('Test data');
console.log('Object mode disabled:', writable.writableObjectMode);
Received: Test data Object mode disabled: false
Key Points
- Object mode allows streams to work with JavaScript objects instead of just strings and Buffers
- The property is read-only and reflects the stream's configuration
- When
undefined, the stream uses default buffer mode - This property is useful for programmatically checking stream behavior
Conclusion
The writableObjectMode property helps determine if a writable stream operates in object mode. Use it to verify stream configuration and ensure compatibility with your data types.
Advertisements
