mongodb throw new error_1.MongoNotConnectedError('客户端必须在运行操作之前连接');

bf1o4zei  于 2023-08-04  发布在  Go
关注(0)|答案(1)|浏览(133)

while i am trying to send post request thunder clinent in localhost:3000/API/auth i am gettung this error,howver my data was send in thunder clinet status but it wasn't send to database and my app crased -

[nodemon] restarting due to changes...
[nodemon] starting `node .\index.js`
Example app listening on port 3000
Connected to MongoDB
[nodemon] restarting due to changes...
[nodemon] starting `node .\index.js`
Example app listening on port 3000
Connected to MongoDB
{ name: 'suman', email: 'suman@gamil.com', password: '1234' }
C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongodb\lib\operations\execute_operation.js:23
            throw new error_1.MongoNotConnectedError('Client must be connected before running operations');
                  ^

MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongodb\lib\utils.js:290:21)
    at executeOperation (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Model.$__save (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongoose\lib\model.js:387    at Model.$__save (C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\mongoose\lib\model.js:387:8)
    at C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\kareem\index.js:387:18
    at C:\Users\lenovo\Desktop\BCA 4th\react course\inotes\backend\node_modules\kareem\index.js:113:15 {
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.16.0
[nodemon] app crashed - waiting for file changes before starting...

字符串

我试图使用mongoose将我的json文件存储在mongodb compass中。我会给予你我的完整代码,所以它可以帮助

const connectToMongoose = require('./db');
const express = require('express')
connectToMongoose();
const app = express()
const port = 3000

app.use(express.json())
//available routes
app.use('/api/auth',require('./routes/auth.js'))
app.use('/api/note',require('./routes/note.js'))

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
const mongoose = require('mongoose');

const connectionString = 'mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1/firstDB';
const connectToMongoose =()=>{
mongoose.connect(connectionString, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
    .then(() => {
      console.log('Connected to MongoDB');
      // Continue with your MongoDB operations
    })
    .catch((error) => {
      console.error('Failed to connect to MongoDB:', error);
    }) 
    .finally(() => {
      mongoose.connection.close();
    });
}

module.exports = connectToMongoose;
const mongoose = require('mongoose');
const { Schema } = mongoose;
const UserSchema = new Schema({
   name:{
    type:String,
    required:true
   },
   email:{
    type:String,
    unique:true,
    required:true
   },
   password:{
    type:String,
    required:true
   },
   date:{
    type:Date,
    default:Date.now
   }

  });
  module.exports=mongoose.model('users',UserSchema)
const express = require('express');
const User = require('../models/Users');
const router= express.Router();

//creating a user post request
router.post('/',(req,res)=>{
    console.log(req.body);
    const user =User(req.body);
    user.save() 
    res.send(req.body);
})
module.exports=router

.

bogh5gae

bogh5gae1#

在成功建立数据库连接后启动应用程序的良好做法。
不要关闭finally块中的连接

相关问题