3

Below is my database coonnection code in node.

var express = require('express')
var mongoose = require('mongoose') 
var cors = require('cors') 

var morgan = require('morgan')
require('dotenv').config()

const app = express()

 //db
mongoose.connect(process.env.DATABASE, {
    useNewUrlParser:true,
    useFindAndModify:true,
    useUnifiedTopology:false,
    useCreateIndex:true,
 })
  .then(() => console.log('DB Connected'))
  .catch(err => console.log('DB Connection Error =>', err))

   //middleware
app.use(express.json({limit:'5mb'}))
app.use(express.urlencoded({extended:'true'}))
app.use(cors({
    origin:["http://localhost:3000"]
}))

app.post("/api/register", (req, res) =>{
    console.log("Register Endpoint =>", req.body);
})

const port = process.env.PORT
app.listen(port, () => console.log(`Server running on port ${port}`))

when I run this code I get errors :

  1. C:\Users\atiqu\merncamp\server\node_modules\mongodb\lib\collection.js:70 pkFactory: db.options?.pkFactory ?? utils_1.DEFAULT_PK_FACTORY, ^

  2. SyntaxError: Invalid or unexpected token at Module._extensions..js (node:internal/modules/cjs/loader:1300:10)

I was trying to connect my database and these errors arised.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • [Please post code/errors/etc as formatted text instead of links to images of formatted text.](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question/285557#285557) – Dave Newton Mar 08 '23 at 16:51
  • If there is a complete stack trace it should be included (as formatted text) as well, preferably with an indication of where in **your** code there's a syntax error. If the syntax error is not in **your** code then it may be a packaging issue, which is a different thing altogether. – Dave Newton Mar 08 '23 at 16:53
  • How can I solve this problem? I have posted my code as formatted text. @DaveNewton – atique zaman Mar 08 '23 at 16:58
  • The entire stack trace should be included as formatted text. If the syntax error is in **your** code it's helpful to be specific with where in your code the error is. If the syntax error is not in **your** code then it may be a packaging issue which means we need to see how the app is being packaged. – Dave Newton Mar 08 '23 at 17:08
  • Since one of the errors explicitly points to a node module it might be useful to include the version of Node you're running as well. – Dave Newton Mar 08 '23 at 17:09
  • v19.5.0. and I dont understand how to include the entire stack. – atique zaman Mar 08 '23 at 17:13
  • When you run the code there's errors: that's the stack trace. Cut and paste it into a code block in the question. – Dave Newton Mar 08 '23 at 17:25
  • SO, the entire stack is too long. I am leaving some :- C:\Users\atiqu\merncamp\server>npm start > server@1.0.0 start > nodemon -r esm server.js [nodemon] 2.0.21 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting `node -r esm server.js` C:\Users\atiqu\merncamp\server\node_modules\esm\esm.js:1 – atique zaman Mar 08 '23 at 18:05
  • The stack trace should be a formatted block in the question. – Dave Newton Mar 08 '23 at 18:06
  • then a huge stack of 299007 characters and then the two errors mentioned in my question – atique zaman Mar 08 '23 at 18:07
  • A 300k stack trace. That seems unlikely for a syntax error. In any case if the syntax error is not in your code then I would expect a packaging error which means we need to see how the app is being packaged. I really feel like I'm repeating myself a lot. – Dave Newton Mar 08 '23 at 18:09
  • How can I show you how my app is being packaged? I apologize for my little learning and thank you so much as you are giving me the time. – atique zaman Mar 08 '23 at 18:17
  • @atiquezaman what is the value off `process.env.DATABASE`? is it only `database name` or a `connection string`? – Tobok Sitanggang Mar 08 '23 at 22:53
  • its a connection string that I put in a .env file. @TobokSitanggang – atique zaman Mar 09 '23 at 05:06

4 Answers4

2

I was facing the same issue. I downgraded my mongoose from v7 to v6. in package.json file I changed "mongoose": "^7.0.1", to "mongoose": "^6.7.3", then run npm install and I was able to connect to the database.

2

Instead of using this:

mongoose.connect(process.env.DATABASE, {
  useNewUrlParser:true,
  useFindAndModify:true,
  useUnifiedTopology:false,
  useCreateIndex:true,
})

Please use:

mongoose.connect(process.env.DATABASE, {})
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Parking Master May 15 '23 at 22:43
0

Had the same issue,

  1. I downgraded mongoose to 6.9.2 in package.json file.
  2. I reinstalled mongoose npm install mongoose

It staerted working after that :) hope this helps

0

I faced the same issue like yours

pkFactory: db.options?.pkFactory ?? utils_1.DEFAULT_PK_FACTORY,

nodemon version issue

I just downgraded my mongoose version to v6.7.3 & my problem is solved. In my case the current stable version is v7.4.3, There are some errors in this version. Just uninstall the current version like yarn remove mongoose or npm uninstall mongoose and install v6.7.3 like yarn add mongoose@6.7.3 or npm i mongoose@6.7.3.