Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'toString' of undefined on function convertDocToObj(doc) #10

Open
senapahlevi opened this issue Jan 21, 2022 · 2 comments

Comments

@senapahlevi
Copy link

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

image

`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;`

@senapahlevi
Copy link
Author

hello any body help me please, when i cloned part 14 repository then npm install all library and package still get same error like these

@jessicagallagher
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants