I'm building a basic blog in Node.js / Express using MongoDB w/ Mongoose ORM.
I have a pre 'save' hook that I'd like to use to auto-generate a blog/idea slug for me. This works fine and well, except for the part where I want to query to see if there are any other existing posts with the same slug before continuing.
However, it appears that this
does not have access to .find or .findOne() and so I keep getting an error.
What's the best way to approach this?
IdeaSchema.pre('save', function(next) { var idea = this; function generate_slug(text) { return text.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-').trim(); }; idea.slug = generate_slug(idea.title); // this has no method 'find' this.findOne({slug: idea.slug}, function(err, doc) { console.log(err); console.log(doc); }); //console.log(idea); next(); });
The save() function triggers validate() hooks, because mongoose has a built-in pre('save') hook that calls validate() . This means that all pre('validate') and post('validate') hooks get called before any pre('save') hooks.
The simple and easy way is to use the $in operator. The $in operator takes an array as its value.
Mongoose requires a connection to a MongoDB database. You can require() and connect to a locally hosted database with mongoose. connect() , as shown below. You can get the default Connection object with mongoose.
Unfortunately, it's not documented very well (no mention of it in the Document.js API docs), but Documents have access to their models through the constructor
field - I use it all the time for logging things from plugins, which gives me access to which model they're attached to.
module.exports = function readonly(schema, options) { schema.pre('save', function(next) { console.log(this.constructor.modelName + " is running the pre-save hook."); // some other code here ... next(); }); });
For your situation, you should be able to do:
IdeaSchema.pre('save', function(next) { var idea = this; function generate_slug(text) { return text.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-').trim(); }; idea.slug = generate_slug(idea.title); // this now works this.constructor.findOne({slug: idea.slug}, function(err, doc) { console.log(err); console.log(doc); next(err, doc); }); //console.log(idea); });
In this
you have got the document, not the model. Method findOne is not present on the document.
If you need the model, you can always retrieve it as is shown here. But more clever would be to just assign the model to a variable at the point of creation. Then use this variable anywhere you desire. If it is in another file, then use module.exports and require to get it anywhere else in your project. Something like this:
var mongoose = require('mongoose'); var Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/dbname', function (err) { // if we failed to connect, abort if (err) throw err; var IdeaSchema = Schema({ ... }); var IdeaModel = mongoose.model('Idea', IdeaSchema); IdeaSchema.pre('save', function(next) { var idea = this; function generate_slug(text) { return text.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-').trim(); }; idea.slug = generate_slug(idea.title); // this has no method 'find' IdeaModel.findOne({slug: idea.slug}, function(err, doc) { console.log(err); console.log(doc); }); //console.log(idea); next(); }); // we connected ok })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With