Mongodb Connection to localhost Database

  mongodb, question

You can already connect to the database locally;
图片描述

import * as mongoose from 'mongoose';
 
 /**
 * Connection
 */
 mongoose.connect(`mongodb://127.0.0.1:27017`);
 
 /**
 * Connection Successful
 */
 mongoose.connection.on('connected', function () {
 Log ('database connection succeeded');
 });
 
 /**
 * abnormal connection
 */
 mongoose.connection.on('error',function (err) {
 Log ('database connection error:'+err);
 });
 
 /**
 * Disconnect
 */
 mongoose.connection.on('disconnected', function () {
 Log ('database disconnected');
 });
 
 module.exports = mongoose;

Through the above code connection but prompt:

图片描述

The connect string in your code is incorrectly written and should be:

mongoose.connect(‘mongodb://localhost/test’);

Indicates a connection to the database test; If the port number is the default, it can be omitted.

The wall cracks suggest that you should read more documents on mongoose’s website, and some blog posts about mongoose are flawed.

http://mongoosejs.com/

For reference.

Love MongoDB! Have fun!


Be there or not, on the 19th, please stamp: ><

MongoDB Online Lecture Series 19- MongoDB 10 Steps to Build a Single View