Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query from within Mongoose pre hook in a Node.js / Express app?

Tags:

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();   }); 
like image 780
doremi Avatar asked Oct 09 '13 20:10

doremi


People also ask

What is the use in pre In Mongoose?

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.

Can I use $in in Mongoose?

The simple and easy way is to use the $in operator. The $in operator takes an array as its value.

Which dependency is required in the Express app to use Mongoose?

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.


2 Answers

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); }); 
like image 142
Chris Vandevelde Avatar answered Sep 22 '22 03:09

Chris Vandevelde


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 }) 
like image 21
Capaj Avatar answered Sep 24 '22 03:09

Capaj