set() 함수

doka won·2021년 8월 2일
0

firebase

목록 보기
1/1
post-thumbnail

set() 함수를 이용해 collection 문서에 데이터를 추가해보자

firebase 문서 : https://firebase.google.com/docs/firestore/manage-data/add-data

exports.setMission2 = functions.https.onRequest((request, response) => {
// Add a new document with a generated id.
  const data = {
    cdt: new Date(),
    done_count: 0,
    icon_photo: "",
    is_complete: true,
    notice_ment: "comocomo!",
    total_count: request.query.total_count,
    type: request.query.type,
    user_list: [],
  };

  db.collection("mission").add(data).then((res) => {
    console.log("Added document with ID: ", res.id);
    response.send(true);
  });
});

나는 문서id를 랜덤으로 생성했기 때문에 자동으로 생성해주는 예시 코드가 있어서 그것을 이용했다.
요청을 받을려면 query를 사용해야 한다고 해서.. total_count, type 부분 수정함

functions에 들어가보면 트리거 부분에 요청 http 주소가 나오는데 그 뒤에
?total_count=0&type=gogo 처럼 붙여서 실행하고 성공하면 response.send로 받는 값이 화면에 나타난다

그리고 collection 문서에 들어가보면 내가 요청한 값들이 들어가 있는것을 확인할 수 있다.

profile
우당탕탕 개발 오류모음집

0개의 댓글