Why can't this server side code find documents in my collection?


#1

Hi,

I am freaking out… I just want to get documents from a collection server side

I have this server side: works fine

import { Meteor } from 'meteor/meteor';
import qlikauth from 'qlik-auth';
import { Apps, TemplateApps } from '/imports/api/apps.js'
import { Customers, dummyCustomers } from '../api/customers.js';
import { Streams } from '/imports/api/streams.js'
import { senseConfig, engineConfig, certs, authHeaders } from '/imports/api/config.js';

Router.route('/sso', function(request, response, next) {

    console.log("Meteor's authentication module qlikAuthSSO.js received the forwarded request from Qlik Sense proxy. Meteor will now look which user is currently logged in, and request a ticket for this ID, and add his group memberships");
    // console.log(request);

    //first find the customers that have a logged in users (mongo returns a complete document)
    var customer = Customers.findOne({ 'users.currentlyLoggedIn': true });
    console.log('the simulated login received from the database: ', customer);

I have this server side (code gets called from a method) --> does return undefined…

import { Meteor } from 'meteor/meteor';
import { Customers } from '/imports/api/customers';

//import config for Qlik Sense QRS
import { senseConfig, certs, authHeaders } from '/imports/api/config.js';
import lodash from 'lodash';
_ = lodash;


export function logoutUser() {
    // var customer = Customers.findOne({ 'users.currentlyLoggedIn': true });
    var customer = Customers.find().fetch();  //returns undefined
	console.log('logout for customer ', customer);

as you can see below: the data is in mongo server side:

customer.js
export const Customers = new Mongo.Collection('customers');


#2

David Weldon [wrote a post] ( https://dweldon.silvrback.com/common-mistakes) about this that I have found very helpful. Basically JavaScript doesn’t block on method execution, so the browser executes your call to Mongo before the data has reached the client, resulting in a null result.
David suggests some helpful solutions, but theTLDR version is to just check for a null result before calling the returned object.

Hope that helps!
Yon


#3

Thank you, the difference here is that this is server side code and that one is sync right?

Verstuurd vanaf mijn iPhone


#4

This works in meteor mongo, but the same code does not work in meteor server side code:

meteor:PRIMARY> db.customers.find(
...    {
...       users: {
...                 $elemMatch: {
...                      currentlyLoggedIn:true
...                 }
...       }
...    }
... )
{ "_id" : "WiQFA8MaYW66DRYKY", "name" : "Shell", "users" : [ { "name" : "John", "group" : "manager", "currentlyLoggedIn" : true, "country" : "j" } ] }
export function logoutUser() {
    var customer = Customers.findOne({
        users: {
            $elemMatch: {
                currentlyLoggedIn: true
            }
        }
    })
    console.log('logout for customer ', customer);

Why not? thank you


#5

ok issue solved. Stupid bug in my code… I already deleted all signed in users and therefore the it returned nothing…


#6

Ok cool. Just answering your question above - yes it’s a client - server fix and for some reason I missed the fact that your issue was a server - server issue. Sorry :frowning: