Issue with function returning undefined, resolved with code changes.
6 replies
Last updated: Aug 23, 2022
I
Hi guys i need some help on why my function returns // undefined.
Aug 23, 2022, 9:46 AM
K
Hey User. 👋• Please kindly group your Slack messages into a single message so we can use a unique thread to help you.
• Try wrapping your code in triple backticks ``` so we have a proper code snippet which is easier to read. :)
• Share a bit more information about which function is not providing the desired result.
• Try wrapping your code in triple backticks ``` so we have a proper code snippet which is easier to read. :)
• Share a bit more information about which function is not providing the desired result.
Aug 23, 2022, 9:46 AM
I
const InternationalData = async () => { const response = await fetch(COUNTRY_API_URL) const mydata = await response.json() await mydata.records.map(transformCountry) // console.log(records) } InternationalData() const appendToFile = (document) => { const docAsNewLineJson = `${JSON.stringify(document)} \n` fs.appendFileSync('ready-for-import.ndjson', docAsNewLineJson, {flag: 'a+'}) } const getCountryDetails = async (countryName) => { try{ const res = await fetch(`<https://api.airtable.com/v0/app5Ol3fLrAFeVfXh/${countryName}?api_key=xxxxxxxx>`) const data = await res.json() // console.log(data) await data?.records?.map(recordsData) }catch(error){ return( count('no data' + error) ) } } // //Fetching degree types const res = await fetch(DEGREE_API_URL) const data = await res.json() const GeneralRec = data?.records.map(e => {return { _id: e.id, _type: 'englishDegree', name: e.fields.Name}}) const recordsData = async ({fields}) => { const schoolTypeID = await fields["English Type of Degree"] const sID = await schoolTypeID.find(id => id || null ) const recs = { schoolType: { _type: "reference", _ref: fields["Min Length"]}, localWardedDegree: fields["Local Awarded Degree"], englishTypeDegree: await GeneralRec.find(e => e.name ? sID===e._id: null), Level: fields.Level, LocalInsName: fields["Local Ins. Name"], Notes: fields.Notes || null, } if(recs && !undefined){ return recs } } const transformCountry = async (data) => { const countryID = data?.id?.toLowerCase() const countryName = data?.fields['Country (en)'] const countrySlug = data?.fields?.slug const records = await getCountryDetails(countrySlug) // returns undefined const country = { _type: 'Intcountry', _id: countryID, name: countryName, records: records // undefined } // appendToFile(country) // console.log(country) // return country }
Aug 23, 2022, 9:48 AM
K
await mydata.records.map(transformCountry)
// return country
transformCountryfunction does not return anything because you commented out the return statement.
await data?.records?.map(recordsData)
Aug 23, 2022, 9:49 AM
I
Thank you so much
user F
. works perfectly, i get different outputs. i.e some data is returned well when i run the file, and on re-run i get different results.Aug 23, 2022, 12:54 PM
I
Is it something you can help with.
Aug 23, 2022, 12:54 PM
K
Errrr, not without a bit more info. 😅
Aug 23, 2022, 12:55 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.