skip to Main Content
const mongoose = require("mongoose");

mongoose.set('strictQuery', false);
mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true});


const fruitSchema = new mongoose.Schema ({
  name:String,
  rating: Number,
  review: String
});

const Fruit = mongoose.model("Fruit", fruitSchema);

const fruit = new Fruit ({
  name: "Apple",
  rating: 7,
  review: "Pretty good"
});

fruit.save();

This is my code.

$ node app.js 
C:UsersAmanDesktopfruitsProjnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:175
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:UsersAmanDesktopfruitsProjnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:175:23)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Node.js v18.14.0

This is the error

I am following an online course and currently facing an error.
Please help me out with this.

2

Answers


  1. const mongoose = require('mongoose');
    mongoose.set('strictQuery', true);
    mongoose.connect('mongodb://127.0.0.1:27017/fruitsDB', {useNewUrlParser:true});
    
    const Fruit = mongoose.model("Fruit",{
      name: String,
      rating: Number,
      review: String});
    
    
    const Fruit = mongoose.model("Fruit", fruitSchema);
    
    const fruit = new Fruit({
      name: "Apple",
      rating: 7, 
      review: "Pretty good"
    });
    
    fruit.save().then(() => console.log('Data Save Successful!'));
    

    Hope this code will work for you.

    And also you may have did one error in insertOne() function, that’s why its throwing an error in the console.
    You must use the this format while using insertOne() function:-

    modelName.insertOne(callback(req, res){
    {condition},
    {query},
    callback(err)
    });
    

    (As per your code, here "modelName" is "Fruit")

    Upvote if this answer looks useful to you 🙂

    Login or Signup to reply.
  2. const mongoose = require('mongoose');
    mongoose.connect('mongodb://127.0.0.1:27017/fruitsDB', {
      useNewUrlParser: true
    });
    
    const fruitSchema = new mongoose.Schema({
      name: String,
      rating: Number,
      review: String
    });
    
    const Fruit = mongoose.model("Fruit", fruitSchema);
    
    const fruit = new Fruit({
      name: "Apple",
      rating: 7,
      review: "Pretty good"
    });
    
    fruit.save().then(() => console.log('Data Save Successful!'));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search