简体   繁体   中英

Meteor.call issue in Meteor?

I need to know about the usage of Meteor.call . I did a simple example as shown below. The problem is that it never goes to insertDetails() . Can you please check the below code and suggest me what to do so that I don't get the Match Failed error.

Client.JS

Meteor.methods
({
    //this method doesn't cal when using meteor.cal
    insertDetails : function(adData, callback)
     {
       console.log(">>>>>>>>>>>>>>>>>>> ******* insertDetails ");
        checkFields(adData);
        var fields = 
        {
            userID: adData.UserID,
            fname: adData.fname,
            lname: adData.lname,
            dob: adData.dob
         };

         return Client.insert(fields, callback);
      }
});


// SERVER-SIDE HELPERS ************************************

var nonEmpty = Match.Where(function(x) {return !!x;});

var checkFields = function(adData)
 {
   console.log(">>>>>>>>>>>>>>>>>>> checkFields ");
   check(adData.userID, nonEmpty);
    check(adData.fname, nonEmpty);

};

Insert.js

if (Meteor.isClient) 
{
  Template.hello.events({
    'submit #addnewuserdetails': function (e,t)

     {

      if (typeof console !== 'undefined')

      console.log(">>>>>>>>>>>>>>>>>>> Add button in details ");

      e.preventDefault();
          saveClientDetails();
    }
  });
}


var saveClientDetails = function() 
{
    console.log(">>>>>>>>>>>>>>>>>>> saveClientDetails ");
   var fields =  {
        //ownerId: Meteor.userId(),
                        UserID : $('#userid').value

                    ,fname : $('#fname').value

            ,lname :$('#lname').value

            ,dob : $('#dob').value


    };
      console.log(">>>>>>>>>>>>>>>>>>> fields.UserID "+fields.UserID);    
        //here cal to above insertDetails()
         Meteor.call("insertDetails", fields, function(err, result)
           {
              if (!err)
                  {
                       console.log(">>>>>>>>>>>>>>>>>>> saveClientDetails Success");
                   } 
               else 
                {
                      console.log(">>>>>>>>>>>>>>>>>>> saveClientDetails ERROR "+err.reason);
                 }
        });

};

The Match Failed error points to invalid data being rejected by your check function. My guess is that the problem is with user id: when you call the method you use UserID parameter, but then you check lowercase userID . Try fixing that and see whether it works. Also, try commenting out check call and see whether the rest of code is running. Also, how do you verify that the method was not called? Notice that log should be visible in the server console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM