basir / next-amazona

next-amazona.vercel.app
289 stars 226 forks source link

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

Open senapahlevi opened 2 years ago

senapahlevi commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.