I have a book schema where each book can belong in a category and subcategory. If a category doesn't exist I create it, if the subcategory doesn't exist I create it. I'm having trouble figuring out the syntax to create the subdirectory once the category is created.
var BookSchema = new Schema({
category : {
name: String,
list: [{
subcategory: {
name: String,
list: [{
books: {
title: String
}
}]
}
}]
}
});
What I have so far
Book.findOne({"category.list.subcategory.name": subcategory}, function(err, subcategory) {
if (err) {
console.log("MongoDB Error: " + err);
return false; // or callback
}
if (!subcategory) {
console.log("No item found, creating subcategory item");
Book.create(
{
'category.list.subcategory.name': subcategory
}, function(err, createdSubcategory) {
if (err) {
console.log("Create Subcategory MongoDB Error: " + err);
return null; // or callback
}
console.log(createdSubcategory)
}
);
}
else {
console.log("Found subcategory item: " + tracksTableItem.for_user);
//Add or update subcategory
}
console.log('-wattttttt-------')
return true; // or callback
});
Except this isn't working, I think because I'm not finding the category to create the subcategory under
via testinggnitset ser
No comments:
Post a Comment