React Native Javascript提取方法失败

yfjy0ee7  于 2023-02-05  发布在  React
关注(0)|答案(2)|浏览(125)

所以我试着用javascript的fetch方法调用一个API,但是总是失败。
我试着这样称呼api

const addressReq = await fetch(`https://vapi.verifyme.ng/v1/verifications/addresses`, {
                    headers:{
                        'Authorization':`Bearer ${AllKeys.verifyNINKey}`,
                        'Content-Type': 'application/json'
                    },
                    method:'POST',
                    body:{
                        'reference':`${id}`,
                        'city':`${city}`,
                        'state':`${state}`,
                        'landmark':`${city} ${estate && estate + ' estate'}`,
                        'lga': `${lga}`,
                        'street':`${street_number} ${street_name} ${estate} ${city}`,
                        "applicant.firstname" : `${displayName}`,
                        "applicant.lastname" : `${lastName}`,
                        "applicant.dob" : `${rows.birth_info}`,
                        "applicant.phone" : `${number}`,
                        "applicant.idType" : 'KYC',
                        "applicant.idNumber" : `${number}`,
                        // "applicant.firstname" : `${displayName}`,
                        // "applicant": {
                        //     'firstname':`${displayName}`,
                        //     'lastname':`${lastName}`,
                        //     'dob':`${rows.birth_info}`,
                        //     'phone':`${number}`,
                        //     'idType':'KYC',
                        //     'idNumber':`${number}`,
                        // }
                    }
                }).then(async(res) => {
                    console.log(await res.json())
                    dispatch({ type:'UPDATE_STATUS',payload: {status:'guarantor'} })
                }).catch(err => console.log(err))

但是不起作用我一直收到400错误

Object {
  "error": "Bad Request",
  "message": Array [
    "applicant should not be null or undefined",
    "state should not be empty",
    "state must be a string",
    "street should not be empty",
    "street must be a string",
    "lga should not be empty",
    "lga must be a string",
  ],
  "statusCode": 400,
}

但是当我使用postman时,它工作了。
我还在pipedream上添加了一个webhook,但我也没有收到,以证明它不起作用。
我已经尝试了所有可能的方法,但是没有得到积极的反馈。我做错了什么吗?

gab6jxml

gab6jxml1#

感谢@Barmar,我所需要做的就是添加JSON.stringify()
结果:

const addressReq = fetch(`https://vapi.verifyme.ng/v1/verifications/addresses`, {
                    headers:{
                        'Authorization':`Bearer ${AllKeys.verifyNINKey}`,
                        'Content-Type': 'application/json'
                    },
                    method:'POST',
                    body:JSON.stringify({
                        'reference':`${id}`,
                        'city':`${city}`,
                        'state':`${state}`,
                        'landmark':`${city} ${estate && estate + ' estate'}`,
                        'lga': `${lga}`,
                        'street':`${street_number} ${street_name} ${estate} ${city}`,
                        "applicant.firstname" : `${displayName}`,
                        "applicant.lastname" : `${lastName}`,
                        "applicant.dob" : `${rows.birth_info}`,
                        "applicant.phone" : `${number}`,
                        "applicant.idType" : 'KYC',
                        "applicant.idNumber" : `${number}`,
                        // "applicant.firstname" : `${displayName}`,
                        // "applicant": {
                        //     'firstname':`${displayName}`,
                        //     'lastname':`${lastName}`,
                        //     'dob':`${rows.birth_info}`,
                        //     'phone':`${number}`,
                        //     'idType':'KYC',
                        //     'idNumber':`${number}`,
                        // }
                    })
                }).then((res) => {
                    console.log(res.json())
                    dispatch({ type:'UPDATE_STATUS',payload: {status:'guarantor'} })
                }).catch(err => console.log(err))
2nbm6dog

2nbm6dog2#

你不必使用异步和等待在这里,删除他们,再试一次,应该工作

const addressReq = fetch(`https://vapi.verifyme.ng/v1/verifications/addresses`, {
                    headers:{
                        'Authorization':`Bearer ${AllKeys.verifyNINKey}`,
                        'Content-Type': 'application/json'
                    },
                    method:'POST',
                    body:{
                        'reference':`${id}`,
                        'city':`${city}`,
                        'state':`${state}`,
                        'landmark':`${city} ${estate && estate + ' estate'}`,
                        'lga': `${lga}`,
                        'street':`${street_number} ${street_name} ${estate} ${city}`,
                        "applicant.firstname" : `${displayName}`,
                        "applicant.lastname" : `${lastName}`,
                        "applicant.dob" : `${rows.birth_info}`,
                        "applicant.phone" : `${number}`,
                        "applicant.idType" : 'KYC',
                        "applicant.idNumber" : `${number}`,
                        // "applicant.firstname" : `${displayName}`,
                        // "applicant": {
                        //     'firstname':`${displayName}`,
                        //     'lastname':`${lastName}`,
                        //     'dob':`${rows.birth_info}`,
                        //     'phone':`${number}`,
                        //     'idType':'KYC',
                        //     'idNumber':`${number}`,
                        // }
                    }
                }).then((res) => {
                    console.log(res.json())
                    dispatch({ type:'UPDATE_STATUS',payload: {status:'guarantor'} })
                }).catch(err => console.log(err))

相关问题