ahultgren / monguurl Goto Github PK
View Code? Open in Web Editor NEWAutomatically generate a unique and url-friendly alias/slug and store it in Mongoose
Automatically generate a unique and url-friendly alias/slug and store it in Mongoose
Hi,
sorry to bother you
and thanks for sharing :)
The module works fine on save
but when I update a collection like
exports.post = function(req, res, next, id) {
// http://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id
if (!/^[0-9a-fA-F]{24}$/.test(id)) {
return res.jsonp(404,{ error: 'Failed to load post with id ' + id });
}
Post.findById(id).exec(function(err, post) {
if (err) {
return next(err);
}
if (!post) {
return res.jsonp(404,{ error: 'Failed to load post with id ' + id });
}
req.post = post;
next();
});
};
/**
* Update a post
*/
exports.update = function(req, res) {
var post = req.post;
post = _.extend(post, req.body);
post.save(function(err) {
if (err) {
var errs = Object.keys(err.errors);
if (errs.length > 0){
return res.json(500,{ error: err.errors[errs[0]].message });
}
return res.json(500,{ error: 'Cannot save the post' });
}
res.json(200,post);
});
};
the slug don't get updated.
I checked the documentation but couldn't find a solution, what if the slug's origin field updates and i want the slug to be updated too?
Hi,
Thanks for this amazing module!
I am using mongoose and trying to perform an update with upsert=true option
. Monguurl doesn't generate a slug if a new document is created with this update. How do I solve this issue?
Hi, I have an article schema (see below). It works great for creating slugs on insert, but it is possible to update the slug on title change (update) please?
var mongoose = require('mongoose'),
monguurl = require('monguurl');
ArticleSchema = new mongoose.Schema({
title: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
}
});
ArticleSchema.plugin(monguurl({
source: 'title',
target: 'slug'
}));
module.exports = mongoose.model('Article', ArticleSchema);
Thanks
you should add latiniser function into main app.js , it is about regex.
error: uncaught exception: TypeError: doc.model is not a function
- monguurl.js:89 model.hasUniqueAlias
[firsat.io]/[monguurl]/lib/monguurl.js:89:11
- monguurl.js:64 model.findUniqueAlias
[firsat.io]/[monguurl]/lib/monguurl.js:64:11
- monguurl.js:57 model.makeUniqueAlias
[firsat.io]/[monguurl]/lib/monguurl.js:57:11
- monguurl.js:40 model.<anonymous>
[firsat.io]/[monguurl]/lib/monguurl.js:40:12
- hooks.js:62 _next
[firsat.io]/[hooks-fixed]/hooks.js:62:30
- hooks.js:186 fnWrapper
[firsat.io]/[hooks-fixed]/hooks.js:186:8
- schema.js:832 model.<anonymous>
[firsat.io]/[mongoose]/lib/schema.js:832:7
- hooks.js:62 _next
[firsat.io]/[hooks-fixed]/hooks.js:62:30
- hooks.js:186 fnWrapper
[firsat.io]/[hooks-fixed]/hooks.js:186:8
- schema.js:225 model.fn
[firsat.io]/[mongoose]/lib/schema.js:225:11
- hooks.js:62 _next
[firsat.io]/[hooks-fixed]/hooks.js:62:30
- hooks.js:186 fnWrapper
[firsat.io]/[hooks-fixed]/hooks.js:186:8
- schema.js:200
[firsat.io]/[mongoose]/lib/schema.js:200:17
- index.js:127
[firsat.io]/[mongoose]/[kareem]/index.js:127:16
- next_tick.js:73 _combinedTickCallback
internal/process/next_tick.js:73:7
- next_tick.js:104 process._tickCallback
internal/process/next_tick.js:104:9
Getting the following error:
TypeError: undefined is not a function
at EmbeddedDocument.<anonymous> (/home/sharry/Development/Projects/site/node_modules/monguurl/lib/monguurl.js:40:12)
at _next (/home/sharry/Development/Projects/site/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:62:30)
Model
"use strict";
var mongoose = require('mongoose'),
monguurl = require('monguurl'),
timestamps = require('mongoose-timestamp');
var PageSchema = new mongoose.Schema({
title: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
},
body: {
type: String,
required: true
},
order: {
type: Number,
required: true
},
status: {
type: String,
required: true,
enum: ['live', 'hidden'],
default: 'hidden'
}
});
var SectionSchema = new mongoose.Schema({
title: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
},
excerpt: {
type: String,
required: true
},
order: {
type: Number,
required: true
},
pages: [PageSchema]
});
/**
* Book Schema
*/
var BookSchema = new mongoose.Schema({
contentType: {
type: String,
trim: true,
required: true
},
slug: {
type: String,
index: {
unique: true
}
},
section: [SectionSchema]
});
/////////////////////////////////////////////////////////////
// PLUGINS
/////////////////////////////////////////////////////////////
PageSchema.plugin(monguurl({
source: 'title',
target: 'slug'
}));
SectionSchema.plugin(monguurl({
source: 'title',
target: 'slug'
}));
BookSchema.plugin(monguurl({
source: 'contentType',
target: 'slug'
}));
Controller:
Book.findOne(
{
'slug': 'support'
}
)
.exec(function(err, book) {
if (err) {
return next(err);
} else {
var section = {
title: "Section 1",
excerpt: "Section 1 excerpt here",
order: 1
};
// Create a new section
book.section.push(section);
book.save(function (saveBookErr, book) {
if (saveBookErr) {
return next(saveBookErr);
} else {
res.send('Book updated')
}
});
}
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.