talha-asad / mongoose-url-slugs

Create URL compatiable slugs on mongoose models, ensuring uniqueness.
MIT License
40 stars 22 forks source link

It adds a number to the end when it's not necessary #34

Closed dassennato closed 6 years ago

dassennato commented 7 years ago

Hi,

I'm using 1.0.0 and consider this case:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const URLSlugs = require('mongoose-url-slugs');

let fooSchema = new Schema({
    name: { type: String, required: true },
});

fooSchema.plugin(URLSlugs('name'));

const Foo = mongoose.model('Foo', fooSchema);

Foo.create({
    name: 'Foo Bar'
})
.then((foo) => {
    console.log(foo.slug); // ====> 'foo-bar', this slug is ok!
    return Foo.create({
        name: 'Foo'
    });
})
.then((foo) => {
    console.log(foo.slug); // ====>'foo-2' was created but 'foo' was expected.
});

If I invert the order, I first create 'Foo' and then 'Foo Bar' it works as expected.

Thanks!

talha-asad commented 7 years ago

hey @dassennato,

Seems you've found a bug, I would dig into this further over the week, in the meanwhile if you find a solution, please don't hesitate to submit a PR.

Thanks!

ralphnajm commented 6 years ago

I made a fix https://github.com/talha-asad/mongoose-url-slugs/pull/36

talha-asad commented 6 years ago

Merged and test added