Exie Exie - 3 months ago 19
Node.js Question

Meteor JS - Client Collection seems to be empty

Its been a long time since I used Meteor or dabbled in the UI world for that matter, so forgive me if this is poor question.

api/main.js:

export const Nodes = new Meteor.Collection("nodes");
export const Links = new Meteor.Collection("links");


server/main.js

import { Links, Nodes } from '../api/main.js';

Meteor.startup(() => {
// code to run on server at startup
}

Meteor.methods({
traverseDocument: function traverseDocument(documentKey) {
// ... do stuff ...
Links.insert( data.links );
Nodes.insert( data.nodes );
return data;
}
}


client/main.js

import { Links, Nodes } from '../api/main.js';
import './main.html';

Meteor.startup(function () {
// Fetch data via a call
Meteor.call("traverseDocument", 'NTD000000228506', function (error, results) {});
});

Template.viz.rendered = function () {
console.log(Nodes.find()); // <-- LocalConnection.Cursor()
console.log(Nodes.find().fetch()); // <-- This is empty []
console.log(Nodes._collection._docs._map); // <-- This returns Object{}
}


I've tried:
1. Publish/Subscribe
2. I tried a helper function
3. I Have moved the Meteor.Call() into the client startup function

So this now seems to be closer to working.

The original question changes now to - Why does the find().fetch() return an empty array ?

Maybe I should post a new question and mark this one as fixed ?

Answer

You can save value of results from callback to session variable or reactive variable and then retrieve later.

import { Links, Nodes } from '../api/main.js';
import './main.html';

Template.viz.rendered = function () {
  Meteor.call("traverseDocument", 'VALUE001', function (error, results) {
    if(error) {
      // handle error
    }
    else {
     Session.set('result',results);  // save result in session or reactive       variable
    }
    console.log(Nodes);  // <-- This works, has data
  });

  if(Session.get('result') {
  console.log(Session.get('result')); // <-- Retrieve value from session or reactive var
 }
}