How can I dynamically update a webpage from the server in Meteor?
It seems like it should be simple (and it probably is), but I can't figure it out. I am trying to write a Watson app in Meteor that inserts documents into a corpus. I want to update the client webpage with the name of each document when inserted. I decided that the easiest way to update the web page would be to add a template with {{each}} that iterates through the collection. Then I could just update the collection on the server when I insert documents.
I don't seem to understand how this works. Here's the relevant HTML:
<template name="results">
<table border=0>
{{#each result}}
<tr><td>{{label}}</td></tr>
{{/each}}
</table>
</template>
This is a helper for the template:
Template.results.helpers({
"result":function()
{
return addedDocs.find();
}
})
At the top of the .js file, outside of everything else, is
var addedDocs=new Mongo.Collection("added");
which should make addDocs a global variable.
The server code is located in another file in the server / directory . In a loop that adds documents, I have
addedDocs.insert(doc.label);
which should insert a new mongoDB entry for each document as it is added to the corpus.
I understand that this should all work and the document names should appear on the web page as they are added. The problem is, it won't even compile. I think it has to do with the fact that the server and client code are in separate .js files. Anyway, I get the error
Exception while invoking method 'buildCorpus' ReferenceError: addedDocs is not defined
on the server, which of course means I need to define the addDocs variable in the server's .js file. But nooooo when I define a collection variable on the server
var addedDocs=new Mongo.Collection("added");
I am getting this error instead:
Error: A method named '/added/insert' is already defined
W20150709-14:19:59.386(-5)? (STDERR) at packages/ddp/livedata_server.js:1461:1
W20150709-14:19:59.386(-5)? (STDERR) at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
W20150709-14:19:59.386(-5)? (STDERR) at [object Object]._.extend.methods (packages/ddp/livedata_server.js:1459:1)
W20150709-14:19:59.386(-5)? (STDERR) at [object Object].Mongo.Collection._defineMutationMethods (packages/mongo/collection.js:904:1)
W20150709-14:19:59.387(-5)? (STDERR) at new Mongo.Collection (packages/mongo/collection.js:209:1)
W20150709-14:19:59.387(-5)? (STDERR) at app/ciCorpusBuilder.js:3:15
W20150709-14:19:59.396(-5)? (STDERR) at app/ciCorpusBuilder.js:297:3
W20150709-14:19:59.396(-5)? (STDERR) at /home/david/workspaces/javascript/CI_CorpusBuilder/src/.meteor/local/build/programs/server/boot.js:222:10
W20150709-14:19:59.396(-5)? (STDERR) at Array.forEach (native)
W20150709-14:19:59.396(-5)? (STDERR) at Function._.each._.forEach (/home/david/.meteor/packages/meteor-tool/.1.1.3.4sddkj++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
I get similar errors on the client if I define a collection in the server file but not in the client file.
Am I doing this all wrong? Is there a better way? What am I missing here? I'm pulling my hair out!
source to share