اذهب إلى المحتوى
  • 0

مشكلة في الاتصال مع قاعدة البيانات failed to connect to server في mongodb

Tamim Fahed

السؤال

عندما أقوم بتشغيل خادم الويب من خلال nodemon تظهر لدي أخطاء متعلّقة بالاتصال بقاعدة البيانات mongodb:

DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. 
To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Warning { MongoError: failed to connect to server [mongodb:27017] on first connect
    at Pool.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/topologies/server.js:325:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/connection/pool.js:270:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/connection/connection.js:173:49)
    at Socket.g (events.js:292:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at connectErrorNT (net.js:1025:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'MongoError',
  message: 'failed to connect to server [mongodb:27017] on first connect' }

وكود الاتصال بقاعدة البيانات هو التالي:

MongoClient.connect(myURL, { useNewUrlParser: true }, (err, db) => {
    if (err) throw err;
    console.log('connected successfully to the database');
    db.close();
});

ماهو حل هذه المشكلة؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

بدءً من الإصدار 5.7.1 يجب إضافة القيمة التالية إلى المعامل الثاني عند إنشاء الاتصال:

useUnifiedTopology: true

فيصبح كود الاتصال لديك بالشكل التالي:

MongoClient.connect(myURL, { useNewUrlParser: true }, (err, db) => {
    if (err) throw err;
    console.log('connected successfully to the database');
    db.close();
});

فقد تظهر رسائل تحذيرية عند الاتصال حسب النسخة التي تقوم باستخدامها، لذلك يمكنك إضافة إحدى هذه القيم عند ظهور رسالة تحذيرية عن إحداها:

{ 
        useNewUrlParser: true, 
        useUnifiedTopology: true,
        useCreateIndex: true,
        useFindAndModify: false
    }

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...