Hi!
I’m using Collection2
and SimpleSchema
and I want to be able to remove keys that now contain empty string on an upsert. In other words, if a Document’s field had a String value, and now has ‘’, the whole key should be removed.
removeEmptyString
is set to true for both libraries, so this should just work,
Here’s some very simple code.
client main.html
<body>
Name: <input type="text" id="name"><br>
Id: <input type="text" id="id"><br>
<input type="button" id="add" value="Upsert">
</body>
client main.js
import { Template } from 'meteor/templating';
import './main.html';
Template.body.events({
'click #add'(event, instance) {
const name = $("#name").val();
const id = $("#id").val();
const object = {name:name, id:id};
Meteor.call('upsertObject', object);
}
});
Object Schema
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
export const Objects = new Mongo.Collection('Objects');
export const ObjectsSchema = new SimpleSchema({
name: {
type: String,
optional: true
},
id: {
type: String,
optional: true
},
});
server code to perform upsert
import { Meteor } from 'meteor/meteor';
import { ObjectsSchema, Objects } from './Objects';
Meteor.methods({
'upsertObject'(object) {
//Commenting this out allows empty string to be saved to DB, but doesn't remove key
ObjectsSchema.clean(object);
//Upsert where id matches (not _id)
Objects.upsert({id: object.id}, { $set: object});
}
});
Seems super simple. I can create a Document
{name:'John', id:'1'}
and then try to upsert
as {name:'', id:'1'}
, but the Document
will still have ‘John’ as its name. Why?
Collection2 removeEmptyStrings
Seems like the lib has issues with this https://github.com/aldeed/simpl-schema/issues/69