【问题标题】:Server method to return attribute not working properly Meteor JavaScript返回属性的服务器方法无法正常工作 Meteor JavaScript
【发布时间】:2015-07-02 12:58:30
【问题描述】:

我正在尝试调用服务器方法来返回是否存在隐藏属性。该属性在终端控制台中正确返回,但在客户端未正确返回(返回未定义)。我要返回的属性是tireMarkup

这是我的方法调用:

var currentUserId = this._id;

    Meteor.call('checkMarkup', currentUserId, function(tireMarkupExists) {
        console.log(tireMarkupExists) //returns undefined
        if(!tireMarkupExists) {
            alert('Please enter a tire markup greater than 1 for the customer');
            alert(tireMarkupExists) //returns undefined
        }

这是我的服务器方法:

Meteor.methods({
    'checkMarkup': function(currentUserId, tireMarkupExists) {
        console.log('user? ' + currentUserId); //returns the correct user
        a = Meteor.users.findOne(currentUserId); 
        console.log(a.tireMarkup); //returns the integer value correctly
        if (a.tireMarkup & a.tireMarkup > 1) {
            return (tireMarkupExists);
        }
      }
    });

有什么想法吗?我认为问题与我传递 currentUserIdtireMarkupExists 参数的方式有关。

【问题讨论】:

    标签: javascript meteor client server


    【解决方案1】:

    为什么不对结果返回真/假值,像这样。

    把你的服务器方法改成这个。

    Meteor.methods({
        'checkMarkup': function(currentUserId) {
            console.log('user? ' + currentUserId); //returns the correct user
            a = Meteor.users.findOne(currentUserId); 
            console.log(a.tireMarkup); //returns the integer value correctly
            if (a.tireMarkup & a.tireMarkup > 1) {
                return true;
            }else{
                 return false;
            }
          }
        });
    

    并像这样使用Meteor.call

    Meteor.call('checkMarkup', currentUserId, function(error,result) {
            if(!error){
              if(result === true){
                 console.log("tireMarkupExists");
              }else{
                 cosnole.log("tireMarkupExists dont exist")
              }  
             }else{
                console.log("Opss an error : " error.reason)
             }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-12
      • 2018-01-08
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      相关资源
      最近更新 更多