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

يظهر لدي خطأ MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined عند تشغيل المشروع

Abdelrahman Mostafa10

السؤال

عند تشغيل الخادم أحصل على رسالة الخطأ التالية:

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"

وذلك هو ملف app.js:

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const mongoose = require('mongoose');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const authRouter = require('./routes/auth');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/api/auth', authRouter);
app.use('/users', usersRouter);

mongoose.connect(process.env.DB_URL);

module.exports = app;

ولم أتمكن من حل المشكلة وقد أرفقت مجلد المشروع.

api.rar

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

Recommended Posts

  • 0

يبدو أنك تحاول الاتصال بقواعد البيانات عن طريق جلب خصائص الاتصال من الملف .env ولكن يبدو أنك تحتاج لمكتبة dotenv لتتمكن من ذلك

جرب إضافة الشيفرات التالية

// Require the dotenv module
const dotenv = require('dotenv');
// Load the environment variables from the .env file
dotenv.config();
// Get the connection string from the environment variable
const connectionString = process.env.DB_URL;
// Connect to the database using the connection string
mongoose.connect(connectionString, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log('DB connected!'))
  .catch(err => console.error(err));

لاحظ أننا استخدمنا طريقة promise للتأكد من نجاح الاتصال عن طريق then.

كذلك ربما تحتاج استدعاء أمر الإنصات بنفس الطريقة

// Wait for the app to be ready
app.ready().then(() => {
  // Start the server
  app.listen(PORT, () => 
    console.log(`App listening on port ${PORT}!`));
});

وذلك حتى لا تطلب من التطبيق الإنصات إلا بعد جهوزيته عن طريق استخدام ready
يرجى التجربة، وإبلاغي إذا واجهتك أي مشاكل.

تم التعديل في بواسطة El Sayed El Tohamy
رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...