Publish return less document than expected

Hello all,

Return 2 documents

Meteor.publish("driverVehicles", function(driverId) {
    return Vehicles.find({
        assignedDrivers: {
            $elemMatch: {
                driverId: driverId
            }
        }
    })
});

Return 1 document

Meteor.publish("defaultVehicle", function(driverId) {
    return Vehicles.find({currentDriver: driverId, selected: true});
});

With the above publication, my local mini mongo only have 1 document instead of 2. So when I display data on UI, by subscribing to driverVehicles, it only show 1 document instead of 2.

Why is that? What I did wrong? Please advice, thank you.

Please show also the client-side code, and if possible, the schema for Vehicles collection.

Template.switchVehicles.onCreated = function(){
   Meteor.subscribe("driverVehicles", Meteor.userId());
};

Template.switchVehicles.helpers({
  driverVehicles: function(){
    return Vehicles.find({"assignedDrivers": {$elemMatch: {driverId: Meteor.userId()}}});
  }
});
Template.driverMain.onRendered(function() {
    Meteor.subscribe("defaultVehicle", Meteor.userId());
});

Template.driverMain.helpers({
    defaultVehicle: function() {
      return Vehicles.findOne({currentDriver: Meteor.userId(), selected: true});
    }
});

At the current moment I did not use any schema yet. Thanks.

Then can you show the 2 document objects that should be returned by that query?

So when I display data on UI, by subscribing to driverVehicles, it only show 1 document instead of 2.

Maybe the displaying part of the template is wrong? Can you paste the {{> each}} loop?

<div class="ui container switchVehiclesContainer">
      {{#each vehicle in driverVehicles}}
        {{> vehicleTemplate vehicle=vehicle}}
      {{/each}}
</div>

The above is the Blaze code and I cross check this with my another template that display a list without issue. So should be no problem.

OK, I’ll prepare a repo and check if it’s working at my end.

I tried to replace my publish code with yours. The server/console did not output and log.

I tried the mongodb query at RoboMongo, the 2 document return as expected.

The example below does work properly.

(yes, I removed autopublish)

The result is:

If you replace the ‘qwerty’ in client/main.js with another id like ‘asdfg’, it properly shows 1 item too.

/client/main.html

<head>
  <title>vehicles</title>
</head>

<body>
  {{> vehicles }}
</body>

<template name="vehicles">
  counter: {{counter}}
  {{# each vehicle in vehicles}}
    <p>vehicle id: {{vehicle._id}}</p>
  {{/each}}
</template>

/client/main.js

import { Template } from 'meteor/templating';
import './main.html';

Vehicles = new Mongo.Collection('vehicles')

Template.vehicles.onCreated(() => {
  Meteor.subscribe("driverVehicles", 'qwerty');
});

Template.vehicles.helpers({
  vehicles() {
    var v = Vehicles.find({
      assignedDrivers: {
        $elemMatch: {
          driverId: 'qwerty'
        }
      }
    })
    console.log(JSON.stringify(v.fetch()))
    return v
  },
  counter() {
    return Vehicles.find({
      assignedDrivers: {
        $elemMatch: {
          driverId: 'qwerty'
        }
      }
    }).count()
  }
})

/server/main.js

import { Meteor } from 'meteor/meteor'

Vehicles = new Mongo.Collection('vehicles')

if(Vehicles.find().count() == 0) {
  Vehicles.insert({assignedDrivers: [{driverId: 'qwerty'}, {driverId: 'asdfg'}]})
  Vehicles.insert({assignedDrivers: [{driverId: 'zxcvb'}, {driverId: 'qwerty'}]})
}

Meteor.publish("driverVehicles", function(driverId) {
  return Vehicles.find({
    assignedDrivers: {
      $elemMatch: {
        driverId: driverId
      }
    }
  })
})

It’s the same as my code right? I’m still trying to compare and looking at what is wrong with my code.

Yes it’s more or less the same, just with artificial user _ids instead of those provided by Accounts package.

Then it’s more headache for me. Thanks anyway.

Just to make sure. Are all the documents in the database created with Meteor? Or are some of them made in another tool, like previous version of the website or some Mongo manager?

If that is the case, then some of the user _ids may be not found by Meteor, as they will be in different format.

All created by Meteor. By Meteor Method.

Can you show these two document objects?

Hi, I’ll post the documents later. Now I’m busy at other part that I need to complete urgently. Will back to this part later. Thanks so much.

It’s working fine now. Mlab sandbox issue. Respond extremely slow at the time I’m testing, it makes it looks like no document return. End up my code actually working fine. Wasted lots of time.

Thanks so much for trying your best to help. Much appreciated.

1 Like