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

Error: SubDocs.remove() is not a function #parent.children.id(_id).remove() --> "remove() is not a function" #13284

Closed
2 tasks done
AliQasim165878 opened this issue Apr 17, 2023 · 4 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@AliQasim165878
Copy link

AliQasim165878 commented Apr 17, 2023

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.0.3

Node.js version

19.8.1

MongoDB server version

6.0

Typescript version (if applicable)

No response

Description

1 - This is the code in embedding.js

const mongoose = require("mongoose");

mongoose
  .connect("mongodb://127.0.0.1/playground")
  .then(() => console.log("Connected to MongoDB..."))
  .catch((err) => console.error("Could not connect to MongoDB...", err));

const authorSchema = new mongoose.Schema({
  name: String,
  bio: String,
  website: String,
});

const Author = mongoose.model("Author", authorSchema);

const Course = mongoose.model(
  "Course",
  new mongoose.Schema({
    name: String,
    authors: [authorSchema],
  })
);

async function removeAuthor(courseId, authorId) {
  const course = await Course.findById(courseId);
  console.log(course);
  var theAuthor = course.authors.id(authorId);
  console.log(theAuthor);
  theAuthor.remove();
  console.log(await course.save());
}

removeAuthor("643d59f229408d7d1b928522", "643d5bc8cd1f1d2976ae45fa");

2 - When I run following code
node .\embedding.js

3 - I get the following error
C:\Node\mongo-demo\embedding.js:57
theAuthor.remove();
^

TypeError: theAuthor.remove is not a function
at removeAuthor (C:\Node\mongo-demo\embedding.js:57:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Steps to Reproduce

1 - Create a file embedding.js

const mongoose = require("mongoose");

mongoose
  .connect("mongodb://127.0.0.1/playground")
  .then(() => console.log("Connected to MongoDB..."))
  .catch((err) => console.error("Could not connect to MongoDB...", err));

const authorSchema = new mongoose.Schema({
  name: String,
  bio: String,
  website: String,
});

const Author = mongoose.model("Author", authorSchema);

const Course = mongoose.model(
  "Course",
  new mongoose.Schema({
    name: String,
    authors: [authorSchema],
  })
);

async function removeAuthor(courseId, authorId) {
  const course = await Course.findById(courseId);
  console.log(course);
  var theAuthor = course.authors.id(authorId);
  console.log(theAuthor);
  theAuthor.remove();
  console.log(await course.save());
}

removeAuthor("643d59f229408d7d1b928522", "643d5bc8cd1f1d2976ae45fa");

2 - Run following code on terminal
node .\embedding.js

3 -Get following error
C:\Node\mongo-demo\embedding.js:57
theAuthor.remove();
^

TypeError: theAuthor.remove is not a function
at removeAuthor (C:\Node\mongo-demo\embedding.js:57:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected Behavior

The Author with the specified id (as the second parameter) should be deleted from the Mongoose Collection Courses "from its sub document Authors"

@AliQasim165878 AliQasim165878 changed the title SubDocument.remove() is not a function error Error: SubDocs.remove() is not a function #parent.children.id(_id).remove() --> "remove() is not a function" Apr 18, 2023
@IslandRhythms
Copy link
Collaborator

triple backticks ` on the opening and closing to create a code block

@IslandRhythms IslandRhythms added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Apr 19, 2023
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Apr 19, 2023

I'm reading the docs and don't see anywhere listed where a remove function is described. Here is an alternative do doing what you want.

Edit: My mistake, I see where now. Its in the guide section, not the api section.

const mongoose = require("mongoose");


const authorSchema = new mongoose.Schema({
  name: String,
  bio: String,
  website: String,
});

const Author = mongoose.model("Author", authorSchema);

const Course = mongoose.model(
  "Course",
  new mongoose.Schema({
    name: String,
    authors: [authorSchema],
  })
);
let authorId = '';
let courseId = '';
async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const author = await Author.create({
    name: 'author', bio: 'authorbio', website: 'google.com'
  })

  const entry = await Course.create({
    name: 'Course',
    authors: [author]
  });
  authorId = author._id;
  courseId = entry._id;
}


async function removeAuthor(courseId, authorId) {
  const course = await Course.findById(courseId);
  console.log('Found a course', course);
  course.authors.pull({_id: authorId});
  await course.save();
  console.log('course after deletion', course);
}

async function test() {
  await run();
  await removeAuthor(courseId, authorId);
}

test();

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Apr 19, 2023
@IslandRhythms
Copy link
Collaborator

pull() works just fine

const mongoose = require("mongoose");


const authorSchema = new mongoose.Schema({
  name: String,
  bio: String,
  website: String,
});

const Author = mongoose.model("Author", authorSchema);

const Course = mongoose.model(
  "Course",
  new mongoose.Schema({
    name: String,
    authors: [authorSchema],
  })
);
let authorId = '';
let courseId = '';
async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const author = await Author.create({
    name: 'author', bio: 'authorbio', website: 'google.com'
  })

  const entry = await Course.create({
    name: 'Course',
    authors: [author]
  });
  authorId = author._id;
  courseId = entry._id;
}


async function removeAuthor(courseId, authorId) {
  const course = await Course.findById(courseId);
  console.log('Found a course', course);
  // course.authors.pull({_id: authorId});
  const theAuthor = course.authors.id(authorId);
  console.log('what is the author', theAuthor);
  theAuthor.remove();
  await course.save();
  console.log('course after deletion', course);
}

async function test() {
  await run();
  await removeAuthor(courseId, authorId);
}

test();

@vkarpov15
Copy link
Collaborator

@hasezoey is right, the issue is that remove() has been renamed deleteOne(). theAuthor.remove(); -> theAuthor.deleteOne(); should work 👍

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Apr 23, 2023
@vkarpov15 vkarpov15 removed this from the 7.0.5 milestone Apr 23, 2023
vkarpov15 added a commit that referenced this issue Apr 25, 2023
docs(subdocuments): remove callback usage, use `deleteOne()` rather than `remove()` re: #13284
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants