Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slug is always undefined / null #37

Open
weeco opened this issue Oct 3, 2017 · 7 comments
Open

Slug is always undefined / null #37

weeco opened this issue Oct 3, 2017 · 7 comments

Comments

@weeco
Copy link

weeco commented Oct 3, 2017

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
Copy link
Owner

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
Copy link
Author

weeco commented Oct 3, 2017

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
Copy link
Owner

Please submit a reproduction, the test for this passes.

@PankajGadia
Copy link

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

@d-gubert
Copy link

d-gubert commented Sep 24, 2018

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 😛

@d-gubert
Copy link

d-gubert commented Sep 30, 2018

@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
Copy link
Owner

Sounds like a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants