https://discord.gg/botpress logo
❗❗❗**please help today i have to show
# 💻developers
c
❗❗❗**please help today i have to show demoooo**❗ ❗ Here is what is am facing the issue below is the code i am trying to execute async function getUserByMobile() { try { const mobileNumber = workflow.Phone // Fetch mobile number from workflow variable if (!mobileNumber) { console.error('Mobile number is undefined or empty.') return null } const response = await fetch('https://dummyjson.com/users?limit=1&skip=6') // Replace with actual API URL if (!response.ok) { throw new Error(
HTTP error! Status: ${response.status}
) } const data = await response.json() if (Array.isArray(data.users)) { const normalizedMobileNumber = mobileNumber.replace(/\D/g, '') // Remove non-digit characters const user = data.users.find((user) => user.phone.replace(/\D/g, '') === normalizedMobileNumber) if (user) { workflow.result = { id: user.id, firstName: user.firstName, lastName: user.lastName, email: user.email, phone: user.phone } console.log('User found:', workflow.result) return user } else { workflow.result = null } } console.log('Invalid response format or missing users array.') return null } catch (error) { console.error('Error fetching user data:', error) workflow.result = null return null } } // Example usage getUserByMobile().then((user) => { if (user) { console.log('Stored user details in workflow.result:', workflow.result) } else { console.log('User not found.') } }) From the phone number user enters fetch the details of the user like first name,id etc after fetching it would save in the variable name result it is not storing the value in workflow.result variable i have tried by adding logs but it is not saving in the variable but in the log it is showing that value has been saved and when priting workflow.result it is showing the value please helpp
w
If you print the value in the log, then the value is saved. Sometimes, the value may not appear in the left panel in the studio. My two cents: a function should be self-contained; it receives input (like a phone number) and returns output (either a user or null). After you call it, you can decide what to do with the result. 😉
In execution card you can use axios library for HTTP calls.
here is my version
Copy code
ts
async function getUserByMobile(mobileNumber: string): Promise<object | null> {
  try {
    //const mobileNumber = workflow.Phone // Fetch mobile number from workflow variable
    if (!mobileNumber) {
      console.error('Mobile number is undefined or empty.')
      return null
    }

    const response = await axios.get('https://dummyjson.com/users?limit=10') // Replace with actual API URL

    if (!response.data) {
      // throw new Error(`HTTP error! Status: ${response.status}`)
      console.error(`HTTP error! Status: ${response.status}`)
      return null
    }

    const users = response.data.users

    if (Array.isArray(users)) {
      const normalizedMobileNumber = mobileNumber.replace(/\D/g, '') // Remove non-digit characters
      console.log('normalizedMobileNumber', normalizedMobileNumber)
      const user = users.find((user) => user.phone.replace(/\D/g, '') === normalizedMobileNumber)

      if (user) {
        console.log('User found:', user)
        return user
      } else {
        console.log('User NOT found:')
      }
    }

    //console.log('Invalid response format or missing users array.')
  } catch (error) {
    console.error('Error fetching user data:', error)
  }

  return null
}

// Example usage
getUserByMobile('+61 260-824-4986').then((user) => {
  if (user) {
    workflow.result = {
      id: user.id,
      firstName: user.firstName,
      lastName: user.lastName,
      email: user.email,
      phone: user.phone
    }

    console.log('Stored user details in workflow.result:', workflow.result)
  } else {
    console.log('User not found.')
  }
})
c
Yes, yesterday it was showing in the left panel in the studio, but today I have tried all possible ways to modify the code to display it in the left panel, yet I failed. I thought there might be an issue with my code. As input, I am sending the value of the phone number in the variable workflow.phone, but as output, I would like to return the user details in the variable workflow.result. I have tried to display the variable value in the chat by connecting it with a standard node, but I am still getting a empty object { } .
After trying your code still i am facing same issue PFA Screenshot in left panel nothing is storing in result variable but in logs bottom it is showing details has been assigned also when i am printing workflow.result nothing is printing because it is empty https://cdn.discordapp.com/attachments/1342376891792883763/1342426272265146389/Screenshot_83.png?ex=67b99764&is=67b845e4&hm=4d9577e6e0e006a27f9d65d2af72720c1468c1960a6922a048bcd2b398150595&
w
Don't ask me why but the problem is that
workflow.result
is set in
.then()
try this
Copy code
js
const user = await getUserByMobile('+61 260-824-4986')

if (user) {
  workflow.result = {
    id: user.id,
    firstName: user.firstName,
    lastName: user.lastName,
    email: user.email,
    phone: user.phone
  }

  console.log('1. Stored user details in workflow.result:', workflow.result)
} else {
  console.log('User not found.')
}
6 Views