deno 런타임에서 axios로 mongoDB랑 통신하는데 에러가 계속됩니다. mongoDB API 문서를 잘 숙지하지 않고 있어서 발생하는 문제 같습니다.
문제: 튜토리얼 보고 배우기
- flash-card-backend 프로젝트를 진행하는 중간 발생했습니다.
- deno 런타임에서 mongoDB 통신하는데 axios가 작동하지 않습니다.
- 특이하게 post 요청에는 문제가 없습니다.
- 하지만 get 요청은 정상동작하지 않습니다.
- Getting Started with Deno & MongoDB - MongoDB 이 튜토리얼을 보고 따라 했습니다. 여기서는 모든 요청이 post이고 fetch로 통신했습니다.
- 가독성이 너무 떨어지는 코드였습니다. 그래서 axios를 싱글튼 패턴으로 만들어서 DB에 요청하도록 변경을 시도하고 있었습니다.
시도: axios 싱글튼
전체 코드
import { config } from 'https://deno.land/x/dotenv@v3.2.2/mod.ts';
import axios from 'https://deno.land/x/redaxios@0.5.1/mod.ts';
// axios 싱글튼
const { APP_ID, CARD_API_KEY } = config();
const baseURL = `https://us-west-2.aws.data.mongodb-api.com/app/${APP_ID}/endpoint/data/v1/action`; // /action
type Card = {
question: string;
answer: string;
submitDate: string;
stackCount: number;
};
class AxiosAPI {
private static instance: AxiosAPI;
private axiosConfig;
private query: {
dataSource: string;
database: string;
collection: string;
document?: Card;
};
private constructor() {
this.axiosConfig = axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
'api-key': CARD_API_KEY,
},
method: 'POST',
});
this.query = {
dataSource: 'Cluster0',
database: 'cards_db',
collection: 'cards',
};
}
static getInstance(): AxiosAPI {
if (!AxiosAPI.instance) {
AxiosAPI.instance = new AxiosAPI();
}
return AxiosAPI.instance;
}
async getCards() {
const body = JSON.stringify({});
return await this.axiosConfig.post('/find', {
body: JSON.stringify({
dataSource: 'Cluster0',
database: 'cards_db',
collection: 'cards',
}),
});
}
async postCards(payload: Card) {
this.query.document = payload;
return await this.axiosConfig.post('/insertOne', this.query);
}
async patchCards() {}
async deleteCards() {}
async singin() {}
async singup() {}
}
const axiosLog = AxiosAPI.getInstance();
try {
console.log(await axiosLog.getCards());
} catch (error) {
console.log(error);
}
export default AxiosAPI;
이것이 전체 코드입니다.
반환하는 에러 메시지는 다음입니다.
"Invalid parameter(s) specified: body"
요청을 보낼 때 body 문제라는 것은 파악할 수 있습니다. 하지만 body에 어떻게 변형해도 답을 얻을 수 없었습니다.