You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hello mr.bassir please helpme to finish your course without any error, before i followed your part 14 tutorial my code run well connect and can read data.js with mongoDB but when i followed your code on part 14 im stuck getting error
here the output
`import mongoose from "mongoose";
const connection = {};
async function connect() {
if (connection.isConnected) {
console.log("already connected");
return;
}
if (mongoose.connections.length > 0) {
connection.isConnected = mongoose.connections[0].readyState;
if (connection.isConnected === 1) {
console.log("use previous connection");
return;
}
await mongoose.disconnect();
}
//delete useCreateIndex, useNewUrlParser, useUnifiedTopology because mongoose 6.00 not used anymore
const db = await mongoose.connect(process.env.MONGODB_URI, {});
console.log("new connection");
connection.isConnected = db.connections[0].readyState;
}
async function disconnect() {
if (connection.isConnected) {
if (process.env.NODE_ENV === "production") {
await mongoose.disconnect();
connection.isConnected = false;
} else {
console.log("not disconnected");
}
}
}
I'm also getting errors with the convertDocToObj(doc) function. I'm getting undefined as stated above, and I'm also getting an error saying TypeError: Cannot read properties of null (reading '_id') traced to the function itself in db.js. As the OP did, I've followed the tutorial step by step and confirmed that my code is the exact same as the repo. I searched for some answers before posting the issue, but nothing was really helpful in solving or explaining the error.
hello mr.bassir please helpme to finish your course without any error, before i followed your part 14 tutorial my code run well connect and can read data.js with mongoDB but when i followed your code on part 14 im stuck getting error
here the output
`import mongoose from "mongoose";
const connection = {};
async function connect() {
if (connection.isConnected) {
console.log("already connected");
return;
}
if (mongoose.connections.length > 0) {
connection.isConnected = mongoose.connections[0].readyState;
if (connection.isConnected === 1) {
console.log("use previous connection");
return;
}
await mongoose.disconnect();
}
//delete useCreateIndex, useNewUrlParser, useUnifiedTopology because mongoose 6.00 not used anymore
const db = await mongoose.connect(process.env.MONGODB_URI, {});
console.log("new connection");
connection.isConnected = db.connections[0].readyState;
}
async function disconnect() {
if (connection.isConnected) {
if (process.env.NODE_ENV === "production") {
await mongoose.disconnect();
connection.isConnected = false;
} else {
console.log("not disconnected");
}
}
}
function convertDocToObj(doc) {
doc._id = doc._id.toString();
doc.createdAt = doc.createdAt.toString();
doc.updatedAt = doc.updatedAt.toString();
return doc;
}
const db = { connect, disconnect, convertDocToObj };
export default db;`
The text was updated successfully, but these errors were encountered: