簡體   English   中英

流星:傳遞調用方法結果時發生異常

[英]Meteor: Exception in delivering result of invoking method

已經發布了類似的問題,但沒有一個完全符合我遇到的問題。 我正在對內部服務器進行簡單的POST以獲取產品數據。 調用成功,當我在服務器端執行console.log時,我看到JSON數據正確記錄到我的終端上。 問題出現在客戶端,當在回調中時,結果和錯誤均未定義。

服務器:

Meteor.methods({
    ProductSearch: function(searchTerm) {
        var method = 'POST';
        var url = 'server';
        var options = {
            headers:{"content-type":"application/json"},
            data: { 
                query:"trees"
            }
        };
        return HTTP.call(method, url, options, function (error, result) {
            if (error) {
                console.log("ERROR: ", result.statusCode, result.content);
            } else {
                var txt = JSON.parse(result.content);
                console.log("SUCCESS: Found "+txt.totalResults+" products");
            } 
        });
    }
});

客戶:

Meteor.call('ProductSearch', searchTerm, function (error, result) {
    if (error) {
        console.log("error occured on receiving data on server. ", error );
    } else {
        var respJson = JSON.parse(result.content);
        Session.set("productSearchResults", respJson);
    }
});

, and on callback, they are both undefined, and I get the following error: Exception in delivering result of invoking 'ProductSearch': TypeError: Cannot read property 'content' of undefined 當我記錄的值以及回調的時,它們均未定義,並且出現以下錯誤: 傳遞調用'ProductSearch'的結果時出現異常:TypeError:無法讀取未定義的屬性'content'

在服務器端方法中,由於使用的是異步版本,因此未正確返回HTTP.call的結果,因此HTTP.call將返回undefined ,並且結果只能在回調中訪問。

改用HTTP.call的同步版本, HTTP.call可以了。

try{
  var result = HTTP.call(method, url, options);
  return JSON.parse(result.content);
}
catch(exception){
  console.log(exception);
}

有關其他信息,請參見HTTP.call的相應文檔。

asyncCallback函數

可選的回調。 如果傳遞,該方法將異步運行,而不是同步運行,並調用asyncCallback。 在客戶端上,此回調是必需的。

https://docs.meteor.com/#/full/http_call

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM