talha-asad / mongoose-url-slugs

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

Slug is always undefined / null #37

Open weeco opened 6 years ago

weeco commented 6 years ago

It's the first time I am trying this library, might be pretty useful in many of my projects, however I can't figure out why it's not working at all at the moment.

That's how I define my model in TypeScript:

import * as mongoose from 'mongoose'
import * as URLSlugs from 'mongoose-url-slugs'
const mapSchema = new mongoose.Schema({
  author: {
    type: String,
    required: true,
  },
  name: {
    type: String,
    required: true,
  },
}, { timestamps: true })

mapSchema.plugin(URLSlugs('name'))
const map = mongoose.model('Map', mapSchema)
export default map

And this is the relevant snippet which is executed for upserting the documents:

    const filter = { identifier: document.identifier }
    const options = { runValidators: true, upsert: true }
    const p = Map.findOneAndUpdate(filter, document, options)
    insertPromiseArray.push(p)

The issue:

Unhandled rejection MongoError: E11000 duplicate key error collection: brawlstats.maps index: name_slug_1 dup key: { : null } at Function.MongoError.create (C:\Users\user\Documents\project-csv-converter\node_modules\mongodb-core\lib\error.js:31:11) at C:\Users\user\Documents\project-csv-converter\node_modules\mongodb-core\lib\connection\pool.js:497:72 at authenticateStragglers (C:\Users\user\Documents\project-csv-converter\node_modules\mongodb-core\lib\connection\pool.js:443:16) at Connection.messageHandler (C:\Users\user\Documents\project-csv-converter\node_modules\mongodb-core\lib\connection\pool.js:477:5) at Socket. (C:\Users\user\Documents\project-csv-converter\node_modules\mongodb-core\lib\connection\connection.js:361:20) at emitOne (events.js:115:13) at Socket.emit (events.js:210:7) at addChunk (_stream_readable.js:252:12) at readableAddChunk (_stream_readable.js:239:11) at Socket.Readable.push (_stream_readable.js:197:10) at TCP.onread (net.js:589:20)

It inserts exactly one document into my collection, so I assume this issue pops up because it can't insert a second document with null as key for my slug field. Why is it not sluggifying and inserting my field when I upsert documents?

talha-asad commented 6 years ago

I would advice you setup options, the defaults are causing this error. The plugin is probably adding an index and the values are not unique which is also set as a default.

weeco commented 6 years ago

I do want unique slugs in fact, as far as I understood it would add an incrementing number in case the slug already exists in the schema. As I said I assume it's shown the unique issue because it's trying to use "undefined" for each slug (that only works once since it's supposed to be unique).

Seems to be a bug if you ask me. Maybe it is because I do upserts for saving new documents?

talha-asad commented 6 years ago

Please submit a reproduction, the test for this passes.

PankajGadia commented 6 years ago

Same issue obtained. Getting slug value as undefined for inserting documnet using upsert option.

d-gubert commented 5 years ago

I was having the same issue, but I think I found out how to solve it.

In a first version, I set the plugin like schema.plugin(slug("name")), and everything was cool. Later on I changed it to schema.plugin(slug("store.name", { field: "store.slug" })), and then I started seeing the same error. The problem is that, whenever we assign a field to the plugin, it creates a unique index on the collection. So in my case, it had to indices: one for the field slug and one for the field store.slug, and the one that was giving me the error was acutally the first one, for the field slug, which I wasn't using anymore

Deleting the first index did the job for me, no more errors!

MAYBE the plugin could try to verify if there is a unique "slug" index on the collection that it's not using and delete it automatically, but I'm not really sure how feasible this is. It would have to create indices with a predefined suffix or prefix so it could do that. Not sure if it's worth it.

But I think this can be a common issue during the development phase, so documenting this is a good idea, I guess :stuck_out_tongue:

d-gubert commented 5 years ago

@talha-asad I'm thinking about writing a little Heads Up section on the project's Readme about this specific case, what do you think?

talha-asad commented 5 years ago

Sounds like a good idea.