2017-10-10 81 views
0

我試圖在對象添加屬性是這樣的:流不能指派屬性:計算財產轉讓/元素

request(options:HttpRequestData): Promise<any> { 
    options.headers = options.headers || {}; 

    options.headers['Accept'] = 'application/json'; 
    options.headers['Content-Type'] = 'application/json'; // Error 

    return this._request(options); 
} 

但它的主要錯誤:

options.headers['Content-Type'] = 'application/json'; 
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
assignment of computed property/element. Computed property/element cannot be assigned on possibly undefined value 

HttpRequestData是一個對象類型:

type HttpRequestData = { 
    url:string, 
    method:string, 
    headers?:Object, 
    body?:string, 
}; 

Assiging只是一個值到options.header(在這種情況下,「接受」)的作品,但是當我嘗試失敗兩個或更多。

Flow中的單個賦值和多個賦值以及如何將這些屬性賦值給options.headers對象有什麼區別?

任何建議將非常感激。

+0

看來,沒有類型的對象,在流。我的意思是它的對象類型的語法不正確。 –

+0

@TarasYaremkiv謝謝,所以我只是更新了header的屬性:headers?:{[string]:string}但仍然出現相同的錯誤。 – modernator

回答

0

我解決了這個問題是這樣的:

request(options:HttpRequestData): Promise<any> { 
    options.headers = options.headers || {}; 

    const modifiedHeaders = options.headers; 
    modifiedHeaders['Accept'] = 'application/json'; 
    modifiedHeaders['Content-Type'] = 'application/json'; 

    options.headers = modifiedHeaders; 

    return this._request(options); 
} 

現在沒有更多的錯誤消息。