Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation not updated- 'Model.insertMany() no longer accepts a callback'); #13616

Closed
2 tasks done
arifaisal123 opened this issue Jul 18, 2023 · 0 comments · Fixed by #13635
Closed
2 tasks done

Documentation not updated- 'Model.insertMany() no longer accepts a callback'); #13616

arifaisal123 opened this issue Jul 18, 2023 · 0 comments · Fixed by #13635
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@arifaisal123
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.3.4

Node.js version

18.16.1

MongoDB server version

4.0.28

Typescript version (if applicable)

No response

Description

I have followed the mongoose documentation to use Model.insertMany() in my code.

The documentation example states to use the following:
mongoose1
Considering the documentation, I used the following code for my project:

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const app = express();

app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

mongoose.connect("mongodb://0.0.0.0:27017/todolistDB");

const itemsSchema = {
    name: String
};

const Item = mongoose.model("Item", itemsSchema);

const item1 = new Item({
    name: "Welcome to your todolist!"
});

const item2 = new Item({
    name: "Hit the + button to add a new item"
});

const item3 = new Item({
    name: "<-- Hit this to delete an item."
});

const defaultItems = [item1, item2, item3];

Item.insertMany(defaultItems, function(error, docs) {});

The above code produces the following error:
mongoose2

With stackoverflow, I was able to understand that the callback function with insertMany() method has been deprecated. Hence, I refactored my code following the SO suggestions as below:

Item.insertMany(defaultItems)
            .then(function(){
                console.log("Successfully Inserted!");
            })
            .catch(function(err){
                console.log(err);
            });

The above code seems to solve the issue for me.

I believe the documentation needs to be updated in here.

I am also willing to work and contribute in this issue.

Steps to Reproduce

Running the following code in the terminal with the command "node app.js" reproduces the error.

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const app = express();

app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

mongoose.connect("mongodb://0.0.0.0:27017/todolistDB");

const itemsSchema = {
    name: String
};

const Item = mongoose.model("Item", itemsSchema);

const item1 = new Item({
    name: "Welcome to your todolist!"
});

const item2 = new Item({
    name: "Hit the + button to add a new item"
});

const item3 = new Item({
    name: "<-- Hit this to delete an item."
});

const defaultItems = [item1, item2, item3];

Item.insertMany(defaultItems, function(error, docs) {});

Expected Behavior

I wanted the code to successfully insert the documents in the collection, but it produces the error "MongooseError('Model.insertMany() no longer accepts a callback')"

@vkarpov15 vkarpov15 added this to the 7.4.1 milestone Jul 19, 2023
@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Jul 19, 2023
vkarpov15 added a commit that referenced this issue Jul 20, 2023
docs: rework several code examples that still use callbacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants