0

I am creating MERN stack app I am trying to connect my app with MongoDb database.I am using Mongoose library to connect with `MongoDb.I am getting error below:

Error

TypeError: callback is not a function ['server] at $initialConnection.$initialConnection.then (H:\React projects\MyBlog\node_modules\mongoose\lib\connection.js:744:13) ['server] at process._tickCallback (internal/process/next_tick.js:68:7)

Here is my code below:

server.js

const express = require('express');
const path = require('path');
const cors = require('cors');

const app = express();
app.use(cors());

app.use(require('./routes/saveData.js'));

const port = process.env.PORT || 5000;

app.listen(port,() => {

   console.log(`Server is running on ${port}.`);
});

saveData.js

const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const blogs = require('../models/blogPost');
const mongoose = require('mongoose');

router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended: true}));

const dburl = 'mongodb+srv://example:[email protected]/ExpDb?retryWrites=true'

router.post('/save',(req,res) => {

   const data = {

       title: req.body.title,
       detail: req.body.detail
     }

     const newBlog = new blogs(data);

    mongoose.connect(dburl, {useNewUrlParser: true,useUnifiedTopology:true}).then((resp) =>{

          resp.send("Connected");          
          console.log("connected");
        }).catch((err) => { 
            console.log("database error: ",err);
        });

  });

module.exports = router;

Below is mongoose schema: blogPost.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BlogPostSchema = new Schema({

  title:String,
  body:String,
  date:{
      type:String,
      dafault:Date.now()
    }
});

const BlogPost = mongoose.model('BlogPost',BlogPostSchema);

module.exports = BlogPost;

Someone please let me know what I am doing wrong.Any help would be appreciated.

THANKS

3 Answers 3

1

Mongoose returned promise value is resp. Server response object which is having .send method is res. Try below :

router.post('/save', (req, res) => {
...
    mongoose.connect(dburl, {useNewUrlParser: true, useUnifiedTopology:true}).then((resp) =>{         
        // res not resp
        res.send("Connected");          
        console.log("connected");
    })
Sign up to request clarification or add additional context in comments.

Comments

1

Documentation Reference

Syntax from the documentation,

mongoose.connect(uri, options).then(
  () => { /** ready to use. The `mongoose.connect()` promise resolves to mongoose instance. */ },
  err => { /** handle initial connection error */ }
);

Code

router.post('/save',(req,res) => {

    mongoose.connect(dburl, {useNewUrlParser: true,useUnifiedTopology:true})
      .then(() =>{        
              res.send("Connected");          
              console.log("connected");
            },
           (err)=> console.log("database error: ",err);)        
      });
}

Comments

0

mongoose connect doesn't have a callback

mongoose.connect(dburl, {useNewUrlParser: true},{useUnifiedTopology:true});

this should work. And then to update you should use the mongoose save method on the model object.

BlogPost.save();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.