🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with function returning undefined, resolved with code changes.

6 replies
Last updated: Aug 23, 2022
Hi guys i need some help on why my function returns // undefined.
Aug 23, 2022, 9:46 AM
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.
Aug 23, 2022, 9:46 AM
const InternationalData = async () => {

  const response = await fetch(COUNTRY_API_URL)
  const mydata = await response.json()
  await mydata.records.map(transformCountry)
//  console.log(records)


const appendToFile = (document) => {
  const docAsNewLineJson = `${JSON.stringify(document)} \n`
  fs.appendFileSync('ready-for-import.ndjson', docAsNewLineJson, {flag: 'a+'})

const getCountryDetails = async (countryName) => {

    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)
      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
   await mydata.records.map(transformCountry)
You need to store the result of the map here. Unless your mapper is mutating each country individually but a) I wouldn’t recommend it and b) it doesn’t look like it does.

  // return country
function does not return anything because you commented out the return statement.

     await data?.records?.map(recordsData)
Your function doesn’t return anything because you’re not returning that statement. You’re mapping into the void and not returning anything.
Aug 23, 2022, 9:49 AM
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
Is it something you can help with.
Aug 23, 2022, 12:54 PM
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.

Was this answer helpful?