问题描述
我正在使用MongoDB w /喜欢用于为我自动生成一个博客/想法s。。这样做很好,除了我想查询的部分,看看是否有任何其他现有的帖子与同样的s lug before before before before。。。。。。。。。appears appears appears appears appears appears appears appears appears appears appears appears appears appears appears appears appears appears 这个
无法访问.find或.findOne(),所以我不断收到错误。
这是最好的方法?
IdeaSchema.pre('save',function(next){
var idea = this;
函数generate_slug(text){
return text.toLowerCase()。replace(/ [^ \w] + / g,'' / g,' - ')。trim();
};
idea.slug = generate_slug(idea.title);
//这没有方法'find'
this.findOne({slug:idea.slug},function(err,doc){
console.log(err);
console.log(doc);
});
//console.log(idea);
next();
});
不幸的是,它没有记录很好(没有提到它在中),但文档可以通过构造函数
字段 - 我一直使用它从插件记录的东西,这使我可以访问他们附加的模型。
module.exports = function readonly(schema,options){
schema.pre('save',function(next){
console.log .constructor.modelName +正在运行预先保存的钩子。);
//其他一些代码在这里...
next();
});
});
对于您的情况,您应该可以做到:
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.constructor.findOne({slug:idea.slug} function(err,doc){
console.log(err);
console.log(doc);
next(err,doc);
});
//console.log(idea);
});
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();
});
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);
});
这篇关于如何从一个Node.js / Express应用程序内的Mongoose pre hook内查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!