Javascript mongo中未创建Meteor.js集合
服务器端代码:Javascript mongo中未创建Meteor.js集合,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,服务器端代码: if (Meteor.isClient) { Meteor.subscribe("messages"); Template.hello.greeting = function () { Messages = new Meteor.Collection("messages"); Stuff = new Meteor.Collection("stuff"); return "Welcome to feelings."; }; Template
if (Meteor.isClient) {
Meteor.subscribe("messages");
Template.hello.greeting = function () {
Messages = new Meteor.Collection("messages");
Stuff = new Meteor.Collection("stuff");
return "Welcome to feelings.";
};
Template.hello.events({
'click input' : function () {
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
var response = Messages.insert({text: "Hello, world!"});
var messages = Messages.find
console.log("You pressed the button", response, Messages, Stuff);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Messages = new Meteor.Collection("messages");
Messages.insert({'text' : 'bla bla bla'});
});
}
客户端代码
<head>
<title>Test</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<h1>Hello World!</h1>
{{greeting}}
<input type="button" value="Click"/>
</template>
问题是:
在javascript控制台中输入消息时,插入{'text':'test'};
或者单击按钮,在该按钮下面写入数据库插入调用
我没有看到mongo中插入的文档。去mongo控制台做show dbs节目
消息为空
我还有一些其他问题,我已经阅读了meteor文档,也在谷歌上搜索过,但我似乎找不到一个明确的答案:
为什么我需要在客户端和服务器代码中声明集合?
我在Template.hello.greeting中声明集合,如果我直接将它放在ifMeteor.isClient块中有什么区别。
有没有计划在meteor-like rails中添加一些应用程序目录结构?模型和模板是分开的吗?我不是说express.js
谢谢。您需要在全局范围内创建MongoDB集合,就像在iClient和iServer范围之外一样。因此,从该helper函数中删除Messages=new Meteor.CollectionMessages并将其置于全局范围内 您不能直接通过客户端执行插入,因为meteor不允许从客户端代码插入数据库。如果仍要从客户端插入/更新,则必须为客户端定义数据库规则,请参阅 或者,首选的方法是创建一个服务器方法来插入文档,并使用Meteor.call从客户端调用它 在Template.hello.greeting内创建集合没有任何意义,因为集合用于在服务器上存储可从客户端访问的数据 更新:Meteor>0.9.1 在Meteor中创建收藏现在是:
Messages = new Mongo.Collection("Messages")
而不是:
您需要在全局范围内创建MongoDB集合,比如在iClient和iServer范围之外。因此,从该helper函数中删除Messages=new Meteor.CollectionMessages并将其置于全局范围内 您不能直接通过客户端执行插入,因为meteor不允许从客户端代码插入数据库。如果仍要从客户端插入/更新,则必须为客户端定义数据库规则,请参阅 或者,首选的方法是创建一个服务器方法来插入文档,并使用Meteor.call从客户端调用它 在Template.hello.greeting内创建集合没有任何意义,因为集合用于在服务器上存储可从客户端访问的数据 更新:Meteor>0.9.1 在Meteor中创建收藏现在是:
Messages = new Mongo.Collection("Messages")
而不是: