サーバー側コード:
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 コンソールで Messages.insert({'text' : 'test test test'}); と入力すると、またはボタンをクリックします。その下にデータベース挿入呼び出しが書かれています
mongo に挿入されたドキュメントが表示されません。mongo コンソールに移動して show dbs を実行すると、メッセージが表示されます (空)
他にもいくつか質問があります。流星のドキュメントを読み、グーグルで検索しましたが、これに対する明確な答えが見つからないようです:
- サーバーコードだけでなくクライアントコードでもコレクションを宣言する必要があるのはなぜですか?
- Template.hello.greeting 内でコレクションを宣言していますが、if(Meteor.isClient) ブロックに直接入れた場合の違いは何ですか。
- 流星のようなレールにアプリのディレクトリ構造を追加する計画はありますか? モデルとテンプレートはどこで分離されていますか? 私はexpress.jsについて話しているのではありません
ありがとう。