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
Comments
triple backticks ` on the opening and closing to create a code block |
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(); |
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(); |
@hasezoey is right, the issue is that |
docs(subdocuments): remove callback usage, use `deleteOne()` rather than `remove()` re: #13284
Prerequisites
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
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
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"
The text was updated successfully, but these errors were encountered: