I have a serious problem with meteor publication/subscription on a project.
I have a Card collection with simple form, with an input name and a submit button. Based on that I have a subscription search-card with an object as unique argument (the search, ex: {name: “foobar”}) who return all matching cards on database.
Client
Template.CardSearchForm.onCreated(function() {
let instance = this
instance.name_field = new ReactiveVar(null)
instance.autorun(function() {
let name = instance.name_field.get()
if (name) {
Meteor.subscribe('cards.search', {name: name}, {
onReady: function() {
console.log("Search " + name + " is ready: ", Card.find({}).count())
}
})
}
})
})
When the form is submitted, I update the ReactiveVar to trigger autorun:
Template.CardSearchForm.events({
'submit form.card-search-form': function(e, instance) {
console.log("Submit: " + $('.search-field[name="name"]').val())
instance.name_field.set($('.search-field[name="name"]').val())
}
})
Server
Meteor.publish('cards.search', function (search) {
console.log("New publication search : ", search)
let query = {name: {$regex:".*" + search.name + ".*"}}
console.log("New publication query : ", query)
console.log(Card.find(query).count() + " cards found")
return Card.find(query)
})
----- Working Result -----
At this point everything is working good, I receive all selected cards, see logs (For requesting “Mountain” and “Forest”):
Server
New publication serch : { name: 'Mountain' }
New publication query : { name: { '$regex': '.*Mountain.*' } }
New publication: 12 cards found
New publication serch : { name: 'Forest' }
New publication query : { name: { '$regex': '.*Forest.*' } }
New publication: 7 cards found
Client
Submit: Mountain
Search Mountain is ready: 12
Submit: Forest
Search Forest is ready: 19 // 12 + 7 ==> OK
----- The problem -----
Now I’ll just update the publication and add a RegExp()
query = {name: { $in:[new RegExp(".*" + search.name + ".*", "i")]}}
And the logs show cards are also found but the client didn’t receive new documents from updated search.
Server
New publication serch : { name: 'Mountain' }
New publication query : { name: { '$in': [ /.*Mountain.*/i ] } }
New publication: 12 cards found
New publication serch : { name: 'Forest' }
New publication query : { name: { '$in': [ /.*Forest.*/i ] } }
New publication: 8 cards found // 1 more because `i` case insensitive
Client
Submit: Mountain
Search Mountain is ready: 12
Submit: Forest
Search Forest is ready: 12 // No new cards.. :(
Sorry for this long post… I block so much hours on it.