某APIを試しててaxiosのconfig指定をミスってたので、自戒の意味を込めて残しておきます。
完全に自分用メモっぽいやつです。
ミスったコードなど
//省略classHoge{constructor(){//省略}//ミスった方methodA(IMAGE_PATH){constfile=fs.createReadStream(IMAGE_PATH);constform=newFormData();form.append('image',file);form.append('entrance','detection');constconfig={'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders()};returnaxios.post(this.URL.RECOGNITION,form,config)}//うまくいった方methodB(IMAGE_PATH){constfile=fs.createReadStream(IMAGE_PATH);constform=newFormData();form.append('image',file);form.append('entrance','detection');constconfig={headers:{'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders(),}}returnaxios.post(this.URL.RECOGNITION,form,config)}}consthoge=newHoge();hoge.methodA();// エラーhoge.methodB();// 成功
axiosのconfigミス
慣れてつかってるうちにconfig=headersっぽいイメージで使ってたみたい。
通常configの中には他にもmethod指定やbodyデータなども入ってくるのでconfig.headers
な指定にしないとですね。
constconfig={'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders()};
constconfig={headers:{'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders(),}}