Open weeco opened 7 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.
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?
Please submit a reproduction, the test for this passes.
Same issue obtained. Getting slug value as undefined for inserting documnet using upsert option.
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:
@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?
Sounds like a good idea.
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:
And this is the relevant snippet which is executed for upserting the documents:
The issue:
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?