Quantcast
Viewing all articles
Browse latest Browse all 271

Reading MongoDB Atlas DB from a Next.js web app

I am trying to read a MongoDB Atlas database from a Next.js app that I started to build on Heroku.For that I am following this document and this other one; but it does not quite go as expected.

Here is the testing code for my React component:

import connectDB from "../../configDB";import mongoose from "mongoose";export default async function ShowShops() {  console.log("BEFORE : calling connectDB !!!!")  await connectDB()  console.log("AFTER : calling connectDB !!!!")  const conn = mongoose.connection;  console.log("AFTER : mongoose.connection !!!!")  const {Schema} = mongoose;  const restoSchema = new Schema({    name: String,    ...    zipCode: String  });  const MyModel = mongoose.model('mycollection', restoSchema);  // In the next line somefakecollection is made up.  // The so-called somefakecollection does not exist.  const MyModel2 = mongoose.model('somefakecollection', restoSchema);  console.log("AFTER : MyModel = mongoose.model !!!!")  const yy = await MyModel.findOne({    name: String,    ...    zipCode: String  })  console.log("MyModel.findOne-done ?????")  console.log("findOne-result : "+yy)  console.log("findOne-result : "+yy?.name)  return (<div>      Hello ShowShops !!</div>  )}

Now, this is what happens:

I can see the console.log traces as I expect.

I can see that an empty collection somefakecollection is created.

(This proves at least that the app is somewhat connected to the DB)

But for the result of findOne, I only see this:

remote:        findOne-result : nullremote:        findOne-result : undefined

I expected the line:

console.log("findOne-result : "+yy)

to give me some significant result about the contents of mycollection , but I get nothing interesting.

What am I missing ? Or do I need to change my code ?


Viewing all articles
Browse latest Browse all 271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>