Mongoose Schema Connection.prototype.asPromise() API
The Mongoose Schema API Connection.prototype.asPromise() method of the Mongoose API is used on the Connection object. It allows us to get the result as a promise if the connection to the MongoDB is successfully connected which means the promise is resolved else the promise is rejected. Let us understand the asPromise() method using an example.
Syntax:
connection.asPromise();
Parameters: This method does not accept any parameter.
Return Value: This method returns a promise that can be handled using then and catch block or using an asynchronous function.
Setting up Node.js Mongoose Module:
Step 1: Create a Node.js application using the following command:
npm init
Step 2: After creating the NodeJS application, Install the required module using the following command:
npm install mongoose
Project Structure: The project structure will look like this:
Example 1: The below example illustrates the basic functionality of the Mongoose Connection asPromise() method, using then and catch block. In this example, we are displaying all the properties resolved by asPromise() method, and accessing the name of the database on the result object.
Filename: app.js
Javascript
// Require mongoose module const mongoose = require( "mongoose" ); // Set Up the Database connection const URI = "mongodb://localhost:27017/w3wiki" const connectionObject = mongoose.createConnection(URI, { useNewUrlParser: true , useUnifiedTopology: true , }).asPromise(); connectionObject.then(result => { console.log(Object.keys(result)); console.log(result.name) }). catch (err => console.log(err)) |
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
[ 'base', 'collections', 'models', 'config', 'replica', 'options', 'otherDbs', 'relatedDbs', 'states', '_readyState', '_closeCalled', '_hasOpened', 'plugins', 'id', '_queue', '_listening', '_connectionString', '_connectionOptions', 'client', '$initialConnection', 'db', 'host', 'port', 'name' ] w3wiki
Example 2: The below example illustrates the basic functionality of the Mongoose Connection asPromise() method. In this example, we are handling the promise using asynchronous function.
Filename: app.js
Javascript
// Require mongoose module const mongoose = require( "mongoose" ); // Set Up the Database connection const URI = "mongodb://localhost:27017/w3wiki" const asPromiseExample2 = async () => { const connectionObject = await mongoose.createConnection(URI, { useNewUrlParser: true , useUnifiedTopology: true , }).asPromise(); console.log(Object.keys(connectionObject)); console.log(connectionObject.port) } asPromiseExample2(); |
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
[ 'base', 'collections', 'models', 'config', 'replica', 'options', 'otherDbs', 'relatedDbs', 'states', '_readyState', '_closeCalled', '_hasOpened', 'plugins', 'id', '_queue', '_listening', '_connectionString', '_connectionOptions', 'client', '$initialConnection', 'db', 'host', 'port', 'name' ] 27017
Reference: https://mongoosejs.com/docs/api/connection.html#connection_Connection-asPromise
Contact Us