With Meteor, I am attempting to gather and store data (name, email, and age) using a form. This information should be saved in a new Meteor collection called "Subscribers". Below is my code:
Template Events (client\views\subscribe_form\subscribe_form.js)
Template.Subscribe.events({
'submit form#subscribe-form': function(event){
// Prevent default browser form submit
event.preventDefault();
// Get values from the form
var subName = $('form#subscribe-form [name=subscribe-name]').val();
var subEmail = $('form#subscribe-form [name=subscribe-email]').val();
var subAge = $('form#subscribe-form [name=subscribe-age]').val();
let subscriberData = {
name: subName,
email: subEmail,
age: subAge,
createdAt: new Date()
};
// Insert subscriber into the collection
Meteor.call('SubscribeNow', subscriberData, function(error, result){
if(error){
// Output error if subscription fails
console.log(error.reason);
} else {
// Success
console.log("Subscription successful");
console.log(subscriberData);
console.log( Subscribers.find() );
}
});
},
});
Server side (server\collections\subscribers.js)
var Subscribers = new Meteor.Collection('subscribers');
Subscribers.allow({
insert: function(){
return true;
}
});
Meteor.methods({
'SubscribeNow': function (subscriberData) {
//check(subscriberData, String);
try {
// Any security checks, such as logged-in user, validating data, etc.
Subscribers.insert(subscriberData);
} catch (error) {
// error handling, just throw an error from here and handle it on client
if (badThing) {
throw new Meteor.Error('bad-thing', 'A bad thing happened.');
}
}
}
});
After entering data in the form and clicking the submit button, the success message appears in the console.log, confirming that the data was captured correctly. However, querying the collection does not display any results.
I have tried checking the collection using a template displaying the Subscribers table, Meteor Toys, and
console.log( Subscribers.find() );
, but no luck. It seems like the form submission works, but the data is not being saved in the collection.
https://i.sstatic.net/5q00N.jpg
Additionally, both autopublish and insecure have been removed.
https://i.sstatic.net/3cP8l.jpg
What could be the issue? As I am still new to Meteor, I may be overlooking something obvious here.
Please let me know if you require more code details. Feel free to provide any suggestions for code enhancement (structure or otherwise).
Thank you in advance!