如何在 Meteor 的相同 collection 字段中添加来自相同表单的两个输入?
How to add two inputs from the same forms in the same collection field in Meteor?
我正在研究 meteor 为我的第一个应用程序提供的教程。所以我想进一步扩展它并有两个文本框,一个用于评论,一个用于评分。
问题是我无法从两种形式中正确获取值(实际上根本无法获取评级值)以便将它们保存在我的数据库中,而且 enter-submit 功能停止工作。
我的 body 事件的 .js 代码是:
Template.body.events({
"submit .new-task": function(event) {
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var text = event.target.text.value;
var rating = event.target.rating.value;
// Insert a task into the collection
Meteor.call("addTask", text, rating);
// Clear form
event.target.text.value = "";
}
});
添加任务:
AddTask: function(text, rating) {
//.....
Tasks.insert({
text: text,
createdAt: new Date(),
owner: Meteor.userId(),
username: Meteor.user().username,
rating: rating
});
}
还有我的HTML:
<form class="new-task">
<h2>What is happening?</h2>
<input type="text" name="text" placeholder="Share your experience!" />
<h2>Rating:</h2>
<input type="text" name="rating" placeholder="insert your rating!" />
</form>
<template name="task">
<li class="{{#if checked}}checked{{/if}}">
{{#if isOwner}}
<button class="delete">×</button>
{{/if}}
<span class="text"><strong>{{username}}</strong> - {{text}}- {{rating}}</span>
</li>
</template>
您的 Meteor 方法 addTask
未定义。请改为调用 Meteor.call("AddTask", text, rating);
,或将您的方法重命名为 addTask
.
例如:
if (Meteor.isClient) {
Template.hello.events({
'click button': function () {
Meteor.call("addTask", "1", 2, function(error){
if (error) alert(error.reason);
});
}
});
}
if (Meteor.isServer) {
Meteor.methods({
addTask: function (text, rating) {
check(text, String);
check(rating, Number);
console.log(text);
console.log(rating);
}
});
}
我正在研究 meteor 为我的第一个应用程序提供的教程。所以我想进一步扩展它并有两个文本框,一个用于评论,一个用于评分。 问题是我无法从两种形式中正确获取值(实际上根本无法获取评级值)以便将它们保存在我的数据库中,而且 enter-submit 功能停止工作。
我的 body 事件的 .js 代码是:
Template.body.events({
"submit .new-task": function(event) {
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var text = event.target.text.value;
var rating = event.target.rating.value;
// Insert a task into the collection
Meteor.call("addTask", text, rating);
// Clear form
event.target.text.value = "";
}
});
添加任务:
AddTask: function(text, rating) {
//.....
Tasks.insert({
text: text,
createdAt: new Date(),
owner: Meteor.userId(),
username: Meteor.user().username,
rating: rating
});
}
还有我的HTML:
<form class="new-task">
<h2>What is happening?</h2>
<input type="text" name="text" placeholder="Share your experience!" />
<h2>Rating:</h2>
<input type="text" name="rating" placeholder="insert your rating!" />
</form>
<template name="task">
<li class="{{#if checked}}checked{{/if}}">
{{#if isOwner}}
<button class="delete">×</button>
{{/if}}
<span class="text"><strong>{{username}}</strong> - {{text}}- {{rating}}</span>
</li>
</template>
您的 Meteor 方法 addTask
未定义。请改为调用 Meteor.call("AddTask", text, rating);
,或将您的方法重命名为 addTask
.
例如:
if (Meteor.isClient) {
Template.hello.events({
'click button': function () {
Meteor.call("addTask", "1", 2, function(error){
if (error) alert(error.reason);
});
}
});
}
if (Meteor.isServer) {
Meteor.methods({
addTask: function (text, rating) {
check(text, String);
check(rating, Number);
console.log(text);
console.log(rating);
}
});
}