Open In App

Node.js prompt.addProperties() Method

Last Updated : 25 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The prompt.addProperties() method is an asynchronous function and this method is used to add properties to an existing object from the command line. This function is used for I/O operations.

Syntax:

prompt.addProperties(object, Array[keys], callbackfunction)

Parameters: This method takes three parameters as shown below:

  1. object: The first parameter is an object which is predefined in the program.
  2. Array: The second parameter is the keys to the given object.
  3. callback function:  callback function to make this function is an asynchronous function.

Installation Module:

  1. You can visit the link to Install this module. You can install this package by using this command.
    npm install prompt
    
  2. After that, you can just create a folder and add a file for example, index.js. To run this file you need to run the following command.

    node index.js
    

Project:

Filename index.js




const prompt = require('prompt');
  
// Start the prompt
prompt.start();
  
// Create an object
const user = {
    name: 'GFG',
    country: 'India'
};
  
// Extending the `user` object
prompt.addProperties(user, ['email', 'age'], (err, user) => {
    if (err) {
        throw err;
    }
  
    // Printing modified object
    console.log(user);
  
});


Step to run this program:
Run the index.js file using the following command:

node index.js

Input:

gfg112@gmail.com
prompt: age: 12

Output:

prompt: email:  test@gmail.com
prompt: age:  12
{ name: 'GFG', country: 'India', email: 'test@gmail.com', age: '12' }

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads