5

मैं टाइपस्क्रिप्ट के लिए नया हूं और मैं async का उपयोग करने और कार्यक्षमता का इंतजार करने की कोशिश कर रहा हूं। मुझे कुछ समय में कुछ एफसीएम नेटवर्क टाइमआउट मिल रहा है और मेरा मानना ​​है कि इसे मेरे वादों को सही तरीके से वापस नहीं करने के साथ करना है।एफसीएम और टाइपस्क्रिप्ट Async Await: नेटवर्क टाइमिंग

यहां पुश अधिसूचना भेजने के लिए मेरा क्लाउड फ़ंक्शन है। दो कार्यों कि इंतजार कीवर्ड का उपयोग कर रहे incrementBadgeCount, और sendPushNotification हैं:

async function incrementBadgeCount(memberID: string): 
    Promise<MemberPushNotificaitonInfo> { 
const fs = admin.firestore(); 
const trans = await fs.runTransaction(async transaction => { 
    const docRef = fs.doc(`member/${memberID}`); 
    return transaction.get(docRef).then(doc => { 
      let count: number = doc.get('badgeCount') || 0; 
      const ids: Object = doc.get('deviceToken'); 
      transaction.update(docRef, {badgeCount: ++count}); 
      const memberPayload = new MemberPushNotificaitonInfo(count, ids); 
      return Promise.resolve(memberPayload); 
    }); 
}); 
return trans 
} 

:

export const pushNotification = functions.firestore 
    .document('company/{companyId}/message/{messageId}/chat/{chatId}') 
    .onCreate(async event => { 

const message = event.data.data(); 
const recipients = event.data.data().read; 
const messageId = event.params.messageId; 

const ids = []; 
for (const key of Object.keys(recipients)) { 
    const val = recipients[key]; 
    if (val === false) { 
     ids.push(key); 
    } 
} 

return await Promise.all(ids.map(async (id) => { 
    const memberPayload = await incrementBadgeCount(id); 
    const memberBadgeNumberString = 
     memberPayload.getBadgeCount().toString(); 

    const senderName = message.sender.name; 
    const senderId = message.sender.id; 
    const senderMemberName = message.senderMember.name; 
    const toId = message.receiver.id; 
    const text = message.text; 
    const photoURL = message.photoURL; 
    const videoURL = message.videoURL; 
    const dealId = message.dealId; 
    const dealName = message.dealName; 

    const payload = { 
     notification: { 
      title: `${senderName}`, 
      click_action: 'exchange.booth.message', 
      sound: 'default', 
      badge: memberBadgeNumberString 
     }, 
     data: { senderId, toId, messageId } 
    }; 

    const options = { 
     contentAvailable: true 
    } 

    ........ 

    const deviceIDs = memberPayload.getDeviceID() 
    return await sendPushNotification(id, deviceIDs, payload, options); 
    })); 
}); 

यहाँ incrementBadgeCount समारोह है कि पेलोड के लिए बिल्ला गिनती बढ़ जाती है और पेलोड के लिए कुछ जानकारी देता है और अंत में sendPushNotification समारोह है कि FCM के साथ इंटरफेस और बंद पेलोड भेजता है और बुरा डिवाइस टोकन को साफ:

async function sendPushNotification(memberID: string, deviceIDs: string[], payload: any, options: any) { 
if (typeof deviceIDs === 'undefined') { 
    console.log("member does not have deviceToken"); 
    return Promise.resolve(); 
} 

const response = await admin.messaging().sendToDevice(deviceIDs, payload, options); 
const tokensToRemove = []; 
response.results.forEach((result, index) => { 
    const error = result.error; 
    const success = result.messageId; 
    if (success) { 
     console.log("success messageID:", success); 
     return 
    } 
    if (error) { 
     const failureDeviceID = deviceIDs[index]; 
     console.error(`error with ID: ${failureDeviceID}`, error); 

     if (error.code === 'messaging/invalid-registration-token' || 
      error.code === 'messaging/registration-token-not-registered') { 
      const doc = admin.firestore().doc(`member/${memberID}`); 
      tokensToRemove.push(doc.update({ 
       deviceToken: { 
        failureDeviceID: FieldValue.delete() 
       } 
      })); 
     } 
    } 
}); 

return Promise.all(tokensToRemove); 
} 

मैं इस टाइपस्क्रिप्ट को कसने पर कुछ मदद की सराहना करता हूं :)

उत्तर

0

सबसे अधिक संभावना है कि आप फायरबेस एपीआई पर कॉल कर रहे हैं जो await एड होना चाहिए, लेकिन ऐसा नहीं है। मैं आपको यह बताने के लिए फायरबेस से परिचित नहीं हूं कि यह कौन सा है, लेकिन ऐसा लगता है कि फायरबेस एपीआई के लिए कोई भी कॉल संभावित रूप से await सक्षम है।

यह वह जगह है जहां आपको सुनिश्चित किया जाता है कि आपके पास फायरबेस के लिए टाइप की गई परिभाषाएं हैं और एक अच्छा संपादक का उपयोग करना आता है। अपने सभी फायरबेस कॉल पर नज़र डालें और सुनिश्चित करें कि उनमें से कोई भी गुप्त रूप से वादा वापस नहीं कर रहा है।

इसके अलावा, आपको यह सुनिश्चित करना चाहिए कि आपके सभी कार्यों और चरों को यथासंभव दृढ़ता से टाइप किया गया है, क्योंकि यह आपको किसी भी समस्या से बचने में मदद करेगा।

तो, निम्नलिखित लाइनों मेरे लिए संदिग्ध लग रही:

fs.doc(`member/${memberID}`); 

transaction.update(docRef, {badgeCount: ++count}); 

const doc = admin.firestore().doc(`member/${memberID}`); 
0

इसका कारण यह है कि आप पुश सूचनाएं भेजने के लिए भी कई http कनेक्शन खोल रहे हैं, आदर्श आप 5,10 के बैच बनाना चाहिए .. धक्का भेजने के लिए ।

कोशिश बदलते,

return await Promise.all(ids.map(async (id) => { ... }); 

को,

while(ids.length) { 
    var batch = ids.splice(0, ids.length >= 5 ? 5 : ids.length); 
    await Promise.all(batch.map(async (id) => { ... }); 
} 
संबंधित मुद्दे