2017-01-29 7 views
0

У меня есть клиент api, который отлично работает, получая информацию от Json на веб-сайте. Я хочу добавить несколько новых строк в этот пакет контента после извлечения из веб-сайта.Добавить данные в Json после извлечения в React Native

Я читал о функции .push, но не знаю, где положить новый формат ES6 и не может отличить любую информацию в Google.

Я использую:

react-native-cli: 2.0.1 
react-native: 0.40.0 

Это код:

const BASE_URL = 'https://example.com/data.json'; 

customData = [ 
    { 
    autor: 'Mike', 
    category: 'category name', 
    } 
] 

function getJsonData() { 
    return fetch(BASE_URL) 
    .then(response => response.json()) 
    .then(data => data.items.item) 
    .then(myData => myData.push(customData)) // <- Trying to add custom data 
    .then(news => news.map(item => { 
     return { 
     autor: item.author, 
     category: item.category, 
     } 
    }) 
) 
    //.then(news => news.push(customData)) // <- Also I tried here 
} 

export { getJsonData } 

ответ

0

Во-первых, data.items.item не возвращает Promise вы можете использовать с then.

Использование асинхронной/ждать, чтобы спасти вас неприятности:

async function getJsonData() { 
    const response = await fetch(BASE_URL); 
    const json = await response.json(); 

    // Not clear what you're trying to accomplish here but should give 
    // you an idea 
    data.items.push(customData); 
    return json.items.map((item) => { 
     return { 
     autor: item.author, 
     category: item.category, 
     } 
    }); 
}