Giter VIP home page Giter VIP logo

monguurl's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

monguurl's Issues

Troubles on update

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.

how to run update slug?

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?

Update a title, that updates the slug

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

doc.model is not a function

 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

Using on embedded documents

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')
                }
            });
        }
    });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.