Issues with using publications with view instead of a collection

so I created a view in my database using the command

db.createView('testViewUSA',"collection", [{
 $group: { 
     _id:null,
      count: { $sum: 1 } }
}, 
{
     $project:{
           _id: "test", count:1 
    }
 }])

I have meteor read it as a collection as so testViewUSA;

import {Mongo} from "meteor/mongo"; 

export const testViewUSA = new Mongo.Collection('testViewUSA');

I also have a publication

Meteor.publish({
 'testViewUSA.all': function(_id) { 
      isUserLoggedIn(this.userId);
      console.log('my view data', testViewUSA._name);
      return testViewUSA.find({ _id }); 
},
});

the subscription being used on the client is here

Template.test.onCreated(function() {
     // filler
     this.subscribe('testViewUSA.all', "test")
    // filer 
});

and a helper on the client

getCount() {
     const countData = testViewUSA.findOne({});
     console.log('countData', countData); 
     return countData; 
},

for some reason the data loads and then after a couple of seconds the web socket returns this message

"{"msg":"removed","collection":"testViewUSA","id":"test"}"

rather than updating when the data changes

can you help me

First of all welcome to the forums.

Before we get down to looking at your code though can you wrap it in triple backticks to improve formatting and allow our eyes the courtesy of syntax highlighting? :smile:

```
const message = 'your code here';
```

Now, off the top of my head I’ve picked out that you are passing an object to Meteor.publish which takes a string as the first parameter, and callback function as the second.

Meteor.publish('testViewUSA.all', function(_id) {
    isUserLoggedIn(this.userId); //totally unnecessary, this.userId will be undefined if user is not logged in
    return testViewUSA.find({ _id });
});

If you can update your post to format the code, I or someone else, may take the time to comb through it and find any additional issues.

1 Like

for the isUserLoggedIn function I didn’t want to do the same copy paste check incase I made a mistake so here is the function it provides the function and the line number of where the function is called

/**
 * Throws an error if the user isn't logged in
 * @param userId {String|null}
 * @throws {UserNotLoggedIn} user needs to be logged in
 */
export function isUserLoggedIn(userId) {
    if(!userId) throw Error(`Not Authorized`);
}

that doesn’t look like a proper query, does it?

it will compile to be

return testViewUSA.find({ "_id": "test" });

if that was the case it wouldn’t work the first time at all. but it works first and then dies

Your definition of the view also didn’t work for me, I believe it has to be like this

db.createView('testViewUSA','collection',[
 {
  $group: {
      _id:null,
       count: { $sum: 1 } 
  }
 },{
      $project:{
            _id: "test", count:1
      }
  }
]);

@jamgold
the view was already created and it works it just doesn’t work with meteor publications