javascript - Meteor.js Collection not being created in mongo -
server side code:
if (meteor.isclient) { meteor.subscribe("messages"); template.hello.greeting = function () { messages = new meteor.collection("messages"); stuff = new meteor.collection("stuff"); return "welcome feelings."; }; template.hello.events({ 'click input' : function () { // template data, if any, available in 'this' if (typeof console !== 'undefined') var response = messages.insert({text: "hello, world!"}); var messages = messages.find console.log("you pressed button", response, messages, stuff); } }); } if (meteor.isserver) { meteor.startup(function () { // code run on server @ startup messages = new meteor.collection("messages"); messages.insert({'text' : 'bla bla bla'}); }); }
client side code
<head> <title>test</title> </head> <body> {{> hello}} </body> <template name="hello"> <h1>hello world!</h1> {{greeting}} <input type="button" value="click"/> </template>
the problem:
when in javascript console type messages.insert({'text' : 'test test test'}); or click button, underneath database insertion call written
i don't see document inserted in mongo. going mongo console , doing show dbs shows messages (empty)
i have few other questions, have read through meteor docs , googled can't seem find clear answer this:
- why need declare collection in client server code?
- i'm declaring collections inside template.hello.greeting, what's difference if put in if(meteor.isclient) block directly.
- is plans of adding app directory structure in meteor rails? models , templates separate? i'm not talking express.js
thanks.
you need create mongodb collection in global scope outside of both isclient
, isserver
scopes. remove messages = new meteor.collection("messages")
helper function , place in global scope.
you cannot perform insertion directly through client, meteor doesn't allows database insertion client code. if still want insert/update client, must define database rules client, see docs.
or preferred way create server method insert document, , call client using meteor.call()
.
creating collections inside template.hello.greeting
doesn't makes sense, since collections used store data on server accessible client.
update: meteor > 0.9.1
creating collections in meteor now:
messages = new mongo.collection("messages")
instead of:
messages = new meteor.collection("messages")
Comments
Post a Comment