为什么我为这些输入元素接收空值?

时间:2021-07-16 04:14:41

I am working on a meteor based application and make updates to my DB based off form inputs. When i look up the data in my mongodb the fields are valued as null which isnt the case since i can see their values posted in the url as well i am the one inputting them. Here is the code i have currently implemented.

我正在研究基于流星的应用程序,并根据表单输入对我的数据库进行更新。当我在我的mongodb中查找数据时,字段的值被视为空,这不是这种情况,因为我可以看到他们的值在网址中发布,我也是输入它们的人。这是我目前实施的代码。

<template name="room">
   <div class="container">
      <br>
      <br>
      <br>
      <br>
      <h1>{{currentUser.username}} Cards</h1>
      <hr>
      <h2>Fill out the form below to add a card</h2>
      <form class="new-card">
         <input type="text" name="notes" placeholder="Enter some notes on the sprint" />
         <input id="good-checkbox" type="checkbox" name="category" value="Good">Good
         <input id="bad-checkbox" type="checkbox" name="category" value="Bad">Bad
         <!-- <button class="new-card" type="submit">Add Card</button>-->
      </form>
      <div class="col-xs-12 col-sm-12">
         {{#each cards}}
         {{> card}}
         {{/each}}
      </div>
      <br>
      <br>
      <br>
      <h1>Revealed Cards</h1>
      <hr>
   </div>
</template>

Meteor.js:

Router.configure({
    layoutTemplate: 'main-layout'
});
Router.route('/', {
    name: 'home',
    template: 'login'
});
cards = new Mongo.Collection("cards");
if (Meteor.isClient) {
    Router.route('/room', {
        name: 'room',
        template: 'room'
    });
    if (!Meteor.user()) {
        if (Meteor.loggingIn()) { /*we dont want to do anything*/ } else {
            alert("Sorry, you must be logged in first to use our services.");
            Router.go('/');
        }
    }
    Template.room.helpers({
        cards: function() {
            //return all cards in db sort by newest
            return cards.find({}, {
                sort: {
                    createdAt: -1
                }
            });
        }
    });
    Template.body.events({
        "submit .new-card": function(event) {
            //prevent the browsers default behavior for this event
            event.preventDefault();
            var notes = event.target.text.value;
            var good = $('#good-checkbox:checked').val();
            var bad = $('#bad-checkbox:checked').val();
            if (!good && !bad || !notes || notes.length === 0) {
                alert("No fields can be left blank");
            } else {
                cards.insert({
                    notes: notes,
                    good: good,
                    bad: bad,
                    createdAt: new Date(),
                    createdBy: Meteor.user().username
                });
            }
            event.target.text.value = "";
        }
    });
    Template.room.events({
        "click #addCard": function() {
            alert("inserted card");
            cards.insert({
                text: null,
                category: null,
                createAt: new Date(),
                createdBy: Meteor.user().username
            });
        }
    });
    Accounts.ui.config({
        passwordSignupFields: "USERNAME_ONLY"
    });
}

if (Meteor.isServer) {
    Meteor.startup(function() {
        // code to run on server at startup
    });
}

1 个解决方案

#1


0  

As Shaunak D already pointed out in the comments, the notes variable in your submit .new-card event handler is undefined. Use event.target.notes.value instead of event.target.text.value.

正如Shaunak D在评论中已经指出的那样,你的提交.new-card事件处理程序中的notes变量是未定义的。使用event.target.notes.value而不是event.target.text.value。

if (Meteor.isClient) {
    Template.room.helpers({
        cards: function() {
            //return all cards in db sort by newest
            return cards.find({}, {
                sort: {
                    createdAt: -1
                }
            });
        }
    });
    Template.room.events({
        "submit .new-card": function(event) {
            //prevent the browsers default behavior for this event
            event.preventDefault();
            var notes = event.target.notes.value;
            var good = $('#good-checkbox:checked').val();
            var bad = $('#bad-checkbox:checked').val();
            if (!good && !bad || !notes || notes.length === 0) {
                alert("No fields can be left blank");
            } else {
                cards.insert({
                    notes: notes,
                    good: good,
                    bad: bad,
                    createdAt: new Date(),
                    createdBy: Meteor.user().username
                });
            }
            event.target.notes.value = "";
        }
    });
}

Here's a MeteorPad.

这是一个MeteorPad。

#1


0  

As Shaunak D already pointed out in the comments, the notes variable in your submit .new-card event handler is undefined. Use event.target.notes.value instead of event.target.text.value.

正如Shaunak D在评论中已经指出的那样,你的提交.new-card事件处理程序中的notes变量是未定义的。使用event.target.notes.value而不是event.target.text.value。

if (Meteor.isClient) {
    Template.room.helpers({
        cards: function() {
            //return all cards in db sort by newest
            return cards.find({}, {
                sort: {
                    createdAt: -1
                }
            });
        }
    });
    Template.room.events({
        "submit .new-card": function(event) {
            //prevent the browsers default behavior for this event
            event.preventDefault();
            var notes = event.target.notes.value;
            var good = $('#good-checkbox:checked').val();
            var bad = $('#bad-checkbox:checked').val();
            if (!good && !bad || !notes || notes.length === 0) {
                alert("No fields can be left blank");
            } else {
                cards.insert({
                    notes: notes,
                    good: good,
                    bad: bad,
                    createdAt: new Date(),
                    createdBy: Meteor.user().username
                });
            }
            event.target.notes.value = "";
        }
    });
}

Here's a MeteorPad.

这是一个MeteorPad。