In this very simple app, insert into DB by submit is not working, help?


#1

Resolutions = new Mongo.Collection(‘resolutions’);

if (Meteor.isClient) {
Template.body.helpers({
resolutions: function(){
return Resolutions.find();
}
});

Template.body.events({
‘submit .new-resolution’: function(event){
var title=event.target.title.value;

  Resolutions.insert({
    title: title,
    createdAt: new Date()
  });
event.target.title.value="";
return false;//avoid refresh
}

});
}

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

resolutions

Welcome

Monthly resolutions

</header>
<ul>
  {{#each resolutions}}
   	{{> resolution}}
  {{/each}}
</ul>
  • {{title}}

  • #2

    Your Template needs to subscribe to a server publish function that exposes the Resolutions data you want.

    if (Meteor.isServer) {
    Meteor.publish('allResolutions', function() {
       return Resolutions.find();
    });
    }
    
    if (Meteor.isClient) {
    Template.onCreated(function() {
       this.subscribe('allResolutions');
    });
    

    }

    Take a look at the relevant section in the guide: https://guide.meteor.com/data-loading.html


    #3

    I still have autopublish on, and can insert into the db from my js console.
    So this is probably not the solution.


    #4

    Can you edit your code so it’s escaped and displayed properly in your post? See the CommonMark quick reference for details. This will make it easier to help troubleshoot, as we can’t see parts of your pasted code.


    #5

    I restarted the server, and now it’s working…
    wierd.
    thanks for the help.


    #6

    this error might have resulted from my mongo db console being opened, at the same time.
    mgiht only accept one connection.
    could this be it?