MeteorJS插入不起作用

时间:2015-11-03 15:35:32

标签: javascript meteor

我删除了自动发布以及不安全的内容。我还创建了一个文件夹客户端和服务器,每个文件夹都有init.js,其中包含客户端代码和服务器代码。

init.js客户端中,我有代码

Template.adminSetup.events({
    'submit #adminFormSetup': function(event) {
        event.preventDefault();
        var adminid = event.target.adminid.value;
        var adminUsername = event.target.adminUsername.value;
        var adminEmail = event.target.adminEmail.value;
        var fname = event.target.fname.value;
        var lname = event.target.lname.value;
        var gender = event.target.gender.value;
        var dateBirth = event.target.dateBirth.value;
        Meteor.call('insertAdminData',adminid,adminUsername,adminEmail,fname,lname,gender,dateBirth);
        console.log(adminid);
        console.log(adminUsername);
        console.log(adminEmail);
        console.log(fname);
        console.log(lname);
        console.log(gender);
        console.log(dateBirth);
        alert("Admin Account Successfully Created!")
        Router.go('/accountChecker');
    }
});

Meteor.subscribe('test');

上面的代码从表单中获取值,现在位于我的 init.js服务器端中:

Meteor.publish('test', function(){
   return Meteor.users.find()
   return AdminAccount.find()
});

Meteor.methods({

    //-------------------------------------------------------------
    //Method that will insert the admin data to the database
    'insertAdminData': function(adminid,adminUsername,adminEmail,fname,lname,gender,dateBirth) {
        AdminAccount.insert({
            adminid: adminid,
            adminUsername: adminUsername,
            adminEmail: adminEmail,
            firstname: fname,
            lastname: lname,
            gender: gender,
            dateBirth: dateBirth
        });
        console.log("you clicked the right button");
    }
    //-------------------------------------------------------------
});

将数据存储在名为AdminAccount的集合中。插入数据时,控制台中没有错误或任何警告信号,但数据不存储在AdminAccount集合中。我不确定代码在哪里出错了。请指教。感谢

1 个答案:

答案 0 :(得分:0)

您的AdminAccount集合尚未发布:

Meteor.publish('test', function(){
   return Meteor.users.find()
   return AdminAccount.find()
});

第一个return阻止了第二个人到达。

您希望从该出版物返回游标数组:

Meteor.publish('test', function(){
   return [Meteor.users.find(), AdminAccount.find()];
});