When there is no database added in the system and when we start the node js server for the first time its give this namespace error Mongodb error ns(namespace not found)
Hi @Henil_Mehta,
Welcome to the MongoDB Community
It usually occurs when you perform actions on collections that don’t exist.
Could you please share the code snippet you are executing and the error log you are getting? It will help the community to assist you better.
Best regards,
Kushagra
1 Like
const mongoose = require("mongoose");
const DB_URL = process.env.MONGO_URI;
const loadModels = require("../req-res/models");
const DeviceModels = require("../req-res/models/Device");
const CounterModels = require("../req-res/models/counter");
const GuestModel = require("../req-res/models/Guest");
const SessionController = require("../req-res/controllers/session");
const CallDetailController = require("../req-res/controllers/callDetail");
const QueueClientController = require("../req-res/controllers/queueClient");
const QueueController = require("../req-res/controllers/queue");
const DeviceController = require("../req-res/controllers/devices");
const UserController = require("../req-res/controllers/users");
const UserProfileController = require("../req-res/controllers/userProfile");
const TransactionController = require("../req-res/controllers/transaction");
const GuestController = require("../req-res/controllers/guests");
const MessagesController = require("../req-res/controllers/message");
module.exports = async () => {
const connect = async () => {
mongoose.Promise = global.Promise;
mongoose
.connect(DB_URL, {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(async () => console.log("MongoDB successfully connected"))
.catch((err) => {
console.log("Error when trying to connect to MongoDB: " + err);
console.log("failed to connect to mongodb, retrying...");
connect();
});
mongoose.set("useCreateIndex", true);
mongoose.set("useFindAndModify", false);
loadModels();
await removeUnusedIndexes();
await resetDatabaseWithDefaultValues();
};
connect();
mongoose.connection.on("error", console.log);
};
removeUnusedIndexes = async () => {
let counterIndexes = await CounterModels.collection.getIndexes();
let deviceIndexes = await DeviceModels.collection.getIndexes();
let guestIndexes = await GuestModel.collection.getIndexes();
if (counterIndexes) {
for (var key in counterIndexes) {
if (key === "id_1_reference_value_1") {
CounterModels.collection.dropIndex(key, function (err, result) {
if (err) {
console.log("Error in dropping index!", err);
}
});
}
}
}
if (deviceIndexes) {
for (var deviceIndexesKey in deviceIndexes) {
if (deviceIndexesKey === "userName_1") {
DeviceModels.collection.dropIndex(
deviceIndexesKey,
function (err, result) {
if (err) {
console.log("Error in dropping index!", err);
}
},
);
}
if (deviceIndexesKey === "email_1") {
DeviceModels.collection.dropIndex(
deviceIndexesKey,
function (err, result) {
if (err) {
console.log("Error in dropping index!", err);
}
},
);
}
}
}
if (guestIndexes) {
for (var guestIndexesKey in guestIndexes) {
if (guestIndexesKey === "email_1") {
GuestModel.collection.dropIndex(
guestIndexesKey,
function (err, result) {
if (err) {
console.log("Error in dropping index!", err);
}
},
);
}
}
}
await DeviceModels.syncIndexes();
await CounterModels.syncIndexes();
await GuestModel.syncIndexes();
};
resetDatabaseWithDefaultValues = async () => {
await SessionController.resetCallInfo();
await SessionController.sessionNameIdMigration();
await SessionController.sessionAvatarToDocumentv2Migration();
await CallDetailController.callJoinerIdToCallDetailsMigration();
await QueueClientController.deleteAllQueueClient();
await CallDetailController.resetCallDetailCallingToMissed();
await CallDetailController.resetCallDetailInCallToFailure();
await CallDetailController.resetCallJoinerCallingToMissed();
await TransactionController.processUnresolvedTransactions();
// await QueueController.queueContentMigration();
// await QueueController.queueContentMigration1();
// await DeviceController.deviceDeletedPropertyMigration();
// await UserController.roleMigration();
// await UserController.activeStatusMigration();
// await UserController.inviteStatusMigration();
// await GuestController.rectifyUserName();
// await MessagesController.syncSchemaToDatabase();
await UserController.statusMigration();
await UserController.userAvatarToDocumentv2Migration();
await UserProfileController.userProfileAvatarToDocumentv2Migration();
};
ensure that the Node.js is establish its connection to MongoDB before try to perform any function.
If your server access the database before connection is established, it result in namespace error.
2 Likes