Cloud Functions : 완료 또는 완료되지 않은 작업에 대해 .then ()을 처리하는 방법은 무엇입니까?

알리 톤 올리베이라

제거 할 노드가있는 경우 status노드 값을 업데이트하려고합니다 ..child("Merchans").child(userId).child(merchanId)ref

그러나 반환 된 promise 는 노드가 존재하거나 존재하지 않을 때 remove().then()항상 정의되지 않습니다.

exports.cancelAnnouncements = functions.database
.ref('Cancelads/{userId}')
.onCreate((snapshot, context) => {
  const userId = context.params.userId;

  const targetCountry = snapshot.val().country;
  const targetState = snapshot.val().state;
  const startTime = snapshot.val().ini;
  const merchanId = snapshot.val().merchanId;
  const status = snapshot.val().status;

  var ref;
  if (status === 1) {
    const targetRegion = getTargetRegion(targetCountry, targetState);
    ref = admin.database().ref()
    .child('Queue')
    .child(targetRegion)
    .child(startTime.toString())
    .child(merchanId);

    console.log("inside queue");
  } else if (status === 2) {
    ref = admin.database().ref()
    .child('Announcements')
    .child(targetCountry)
    .child(targetState)
    .child(merchanId);

    console.log("inside announcements");
  }

  return ref.remove().then((result) => {
    if (result === null || result === undefined) return console.log("RESULT IS NULL OR UNDEFINED", result);
    return admin.database().ref()
    .child("Merchans")
    .child(userId)
    .child(merchanId)
    .update({status: 3});
  })
  .catch(error => {
    return console.log("error", error);
  });

});

내 질문은 : 노드가 제거되었는지 여부를 확인하는 방법 입니다 . 때문에 .catch()블록 노드가 존재하지 않는 경우 호출되지 않습니다.

알리 톤 올리베이라

@Frank 반 Puffelen이 언급 한, 우리는 먼저 노드가 하나 존재 여부 있는지 확인하는 읽기 작업을 수행해야합니다.

그러면 원하는 작업을 수행 할 수 있습니다. 확실히이 추가 작업은 비용을 증가시킬 것입니다.

다음은이 작업을 수행하는 데 사용한 코드입니다.

return ref.once('value').then(dataSnapshot => {
    if (dataSnapshot.val() !== null) {
      const promise1 = snapshot.ref.remove();
      const promise2 = dataSnapshot.ref.remove();
      const promise3 = admin.database().ref()
      .child("Merchans").child(userId).child(merchanId).update({status: 3});

      console.log('dataSnapshot exists; removing and updating status', dataSnapshot.val());
      return Promise.all([promise1, promise2, promise3]);
      //return dataSnapshot.ref.remove();
    }
    console.log('dataSnapshot is NULL', dataSnapshot.val());
    return snapshot.ref.remove();
  });

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관