Reputation: 974
For some odd reason, my .update
function isn't properly functioning. I thought the selector of this._id
is the issue here, but I tried passing in a raw ID of one of the items in my database instead. With good_input
, the .insert
content is all stored in the database but the .update
is merely skipped looking at the results.
Template.newInstitution.events({
'click #form_institution': function () {
var inst_name = $('#name')[0].value;
var inst_type = $('#type')[0].value;
var inst_school = $('#school')[0].value;
var inst_desc = $('#description')[0].value;
var good_input = true;
if (inst_name === "") {
$('#name')[0].placeholder = "Please enter a name";
$('#nameDiv').addClass("has-error");
good_input = false;
}
if (inst_type === "") {
$('#type')[0].placeholder = "Please enter a type";
$('#typeDiv').addClass("has-error");
good_input = false;
}
if (inst_school === "") {
$('#school')[0].placeholder = "Please enter your affiliated school";
$('#schoolDiv').addClass("has-error");
good_input = false;
}
if (inst_desc === "") {
$('#description')[0].placeholder = "Please enter a description";
$('#descDiv').addClass("has-error");
good_input = false;
}
if (good_input) {
Institutions.insert({
admin: Meteor.userId(),
title: inst_name,
type: inst_type,
school: inst_school,
description: inst_desc
});
Institutions.update({_id: this._id},
{$push: {
members: Meteor.userId()
}}
);
Router.go('songs');
}
}
});
https://bpaste.net/show/befaba419c19
Would someone identify the issue here and explain the cause of it?
Upvotes: 0
Views: 388
Reputation: 1468
Where do you think that this._id
is being set? Unless there is some further context not visible, there's no such thing. While I agree that you should just include the data in the initial insert
, probably what you're looking for is var id = Institutions.insert({...})
and using id
instead of this._id
.
Upvotes: 1
Reputation: 974
This was the fix. Basically, I can just set an array in the original statement and use $push later.
Template.newInstitution.events({
'click #form_institution': function () {
var inst_name = $('#name')[0].value;
var inst_type = $('#type')[0].value;
var inst_school = $('#school')[0].value;
var inst_desc = $('#description')[0].value;
var good_input = true;
if (inst_name === "") {
$('#name')[0].placeholder = "Please enter a name";
$('#nameDiv').addClass("has-error");
good_input = false;
}
if (inst_type === "") {
$('#type')[0].placeholder = "Please enter a type";
$('#typeDiv').addClass("has-error");
good_input = false;
}
if (inst_school === "") {
$('#school')[0].placeholder = "Please enter your affiliated school";
$('#schoolDiv').addClass("has-error");
good_input = false;
}
if (inst_desc === "") {
$('#description')[0].placeholder = "Please enter a description";
$('#descDiv').addClass("has-error");
good_input = false;
}
if (good_input) {
Institutions.insert({
admin: Meteor.userId(),
title: inst_name,
type: inst_type,
school: inst_school,
description: inst_desc,
members: [Meteor.userId()]
});
Router.go('songs');
}
}
});
Upvotes: 1