2016-08-24 49 views
1

我嘗試使用從AWS生成的API從本地部分(Objective C)返回到React Native時遇到一些問題API網關。使用AWS Api網關生成的iOS(Objective C)SDK與React Native和回調

我已經在aws中建立了一個簡單的測試網關,並且還爲該資源創建了一個模型,它看起來像這樣。如果沒有模式,我無法讀取從AWS在客觀C.

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "title": "dummyConfigModel", 
    "type": "object", 
    "properties": { 
    "config": { 
     "type": "array", 
     "items": { 
     "type": "object", 
     "properties": { 
      "type": { 
      "type": "string" 
      }, 
      "value": { 
      "type": "string" 
      }, 
      "placeholder": { 
      "type": "string" 
      }, 
      "label": { 
      "type": "string" 
      }, 
      "meta": { 
      "type": "object", 
      "properties": { 
       "height": { 
       "type": "integer" 
       }, 
       "width": { 
       "type": "integer" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

好的響應,所以用在地方,我再產生從AWS的API,並得到它安裝在我的工作空間。我遵循http://facebook.github.io/react-native/docs/native-modules-ios.html的文檔來調用我的本地模塊(帶回調)。

const NFAWSManager = NativeModules.NFAWSManager; 
    const config = NFAWSManager.call(
     'configGet', { 
     'dummyKey': 'dummyValue' 
     }, 
     this.callback); 

我的調用由字符串AWS法規劃,以避免對每一個變化的API建立新的本地方法,但對於現在的呼叫方法只是執行「configGet」。

的本地部分看起來是這樣的:

#import "NFAWSManager.h" 
#import "NFReactTestAPIClient.h" 

@implementation NFAWSManager 

RCT_EXPORT_MODULE(); 

RCT_EXPORT_METHOD(
    call:(NSString *)method 
    params:(NSDictionary *)params 
    callback:(RCTResponseSenderBlock)callback 
) 
{ 
    NSLog(@"Method was: %@", method); 
    NSLog(@"Params was: %@", params); 

    NFReactTestAPIClient *client = [NFReactTestAPIClient defaultClient]; 
    [[client configGet:nil] continueWithBlock:^id(AWSTask *task) { 
    if (task.error) { 
     NSLog(@"Error: %@", task.error); 
     callback(@[task.error]); 
    } 
    else { 
     NFDummyConfigModel *output = task.result; 

     NSLog(@"DEBUG %@", output); 

     callback(@[[NSNull null], output]); 
    } 

    return nil; 
    }]; 
} 
@end 

好,所以當我把這個從我做出反應基本上什麼也沒得到。我得到「{}」。但是,如果我硬編碼像字符串或字典這樣的東西,那麼React通常會收到它。

我想我需要幫助的是將task.result對象轉換爲可傳遞給React的格式。我懷疑它可能是模型?

+0

看起來像通過在API網關的響應部分完全刪除模型將結果轉換爲可傳遞的結果。但是,通過刪除這個,我懷疑API的Java版本會出現問題。 *我會繼續挖掘* – fmortens

回答

0

這個問題實際上是AWS中的默認模型是Empty模型,然後導致數據成爲生成的NFEmpty的實例。通過創建一個合適的模型,這已經解決。

刪除模型確實在iOS上有效,但僅用於獲取數據,如果我想發佈模型將是絕對必要的。