'Error: {"data": "foreach() argument must be of type array|object, string given", "message": "" in React Native
i have to send this Array object to the API when i post the API data it shows me this error: Error: {"data": "foreach() argument must be of type array|object, string given", "message": ""
this is my code where i select postcodes
<Picker style={GlobalSS.picker}
selectedValue={postcodes}
mode='dropdown'
dropdownIconRippleColor='orange'
dropdownIconColor='#FF8025'
onValueChange={(itemValue, itemIndex) =>
setPostcodess(itemValue)
}>
<Picker.Item color='grey'
label="Select Postal Code " value="" />
{data3.map(item => (
<Picker.Item label={item.text} value={item.id} />
))}
</Picker>
this is the code where i fetch the postal codes from API
useEffect(() => {
const fetchstateData = async () => {
const resMr = await axios.post
(`${urldemo}getskills`)
setData1(resMr.data.result)
const Org = await axios.post
(`${urldemo}job/job-organization/index?token=${user_token}`)
setJob_organization_id(Org.data.result.job_organization_id)
const responsestate = await axios.post
(`${urldemo}states`)
setData2(responsestate.data.result)
const responsepostal = await axios.post
(`${urldemo}postal-codes`)
setData3(responsepostal.data.result)
}
fetchstateData();
}, []);
const [data1, setData1] = useState([])
const [data2, setData2] = useState([])
const [data3, setData3] = useState([])
const [job_organization_id, setJob_organization_id] = useState("")
const [state_id, setStateId] = useState([]);
const [postcodes, setPostcodess] = useState([]);
const [skills, setSkills] = useState([]);
i want to send post codes in this form
"postcodes": {
"postcode_0": {
"postcode_id": "postcode-0001"
},
"postcode_1": {
"postcode_id": "postcode-0002"
}
},
send Post code and other data here
let data = {
address: address,
job_organization_id: job_organization_id,
customer_id: customer_id,
job_title: job_title,
job_description: job_description,
max_salary: max_salary,
min_salary: min_salary,
job_experience_id: experiance,
job_salary_type_id: salary,
skills: skills,
employment_type_id: jobMode,
job_contact_number: job_contact_number,
job_contact_email: job_contact_email,
job_type_id: jobType,
state_id: state_id,
postcodes: postcodes,
position_no: position_no,
last_apply_date: last_apply_date,
};
fetch(`${urldemo}job/store?token=${user_token}`, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': "application/json",
"Accept": "application/json",
},
})
.then((response) => response.json())
.then((responseJson) => {
console.log("response.JSON ==>", responseJson);
showSuccess('Posted')
if (responseJson.status === 'success') {
console.log(
'Job Posted'
);
showSuccess('Successfully Posted')
setModalVisible(true)
} else {
showError("Not Posted ! something went wrong")
}
})
.catch((error) => {
console.error("Error Received ", error);
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|