Hi!
I’m using SimpleSchema
for modeling a Collection
.
I’m retrieving data from an API and have an edge case.
-
Document.foo
gets set to"bar"
after first request - “foo” in external API gets deleted
- 2nd API request is made and “foo” comes over as “” (empty string)
- Call
CollectionSchema.clean(Document)
- App upserts new
Document
instance wherefoo
should be''
instead ofbar
- Bug:
bar
is left as the value forDocument.foo
.
So issue seemed simple enough in that SimpleSchema didn’t want to insert ''
into the DB and just retained its original value.
However, I noticed that clean
has a removeEmptyString
option set to true by default.
[https://github.com/aldeed/simpl-schema#cleaning-objects](http://SimpleSchema Clean)
Therefore, Document.foo
should just be deleted.
Based on my debugging, this seems to work. I did a console log of Document.foo
after the call to clean
and it shows up as undefined
indicating the property
was removed.
I then printed out the same thing after the upsert
call and verified the original value in the DB is what was still there. So seems like issue is with the Mongo.upsert
const docUpsert= Collection.upsert( { someUniqueKey: apiDoc.someUniqueKey }, { $setOnInsert: { createdBy: Meteor.userId(), createdDate: new Date() }, $set: apiDoc });
The $set
seems to be ignoring/overriding the new empty string value for foo
and SimpleSchema's
rules.
I’m seeing collection2
also has its own removeEmptyStrings
option and that its default is also true. So this should’ve just always worked. My guess is it’s something with a mixture of using SimpleSchema
to clean and then collection2
on the upsert
which does its own cleaning/validating.
Even setting { removeEmptyStrings: true }
in the upsert
call does not work. Could this be a bug for upsert
?
Does anybody know how to resolve this? Thanks!