当firebase云函数触发firestore(onUpdate)时,显示typescript错误 exports.archiveChat=functions.firestore .document('chats/{chatId}')) .onUpdate(快照=>{ const data=snap.after.data(); 常数maxLen=100; const msgLen=data.messages.length; const charLen=JSON.stringify(data).length; const batch=db.batch() 如果(charLen>=10000 | | msgLen>=maxLen){ const deleteCount=msgLen-maxLen

当firebase云函数触发firestore(onUpdate)时,显示typescript错误 exports.archiveChat=functions.firestore .document('chats/{chatId}')) .onUpdate(快照=>{ const data=snap.after.data(); 常数maxLen=100; const msgLen=data.messages.length; const charLen=JSON.stringify(data).length; const batch=db.batch() 如果(charLen>=10000 | | msgLen>=maxLen){ const deleteCount=msgLen-maxLen,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,const data=snap.after.data()可以在没有“after”快照的情况下返回未定义的。只要您的TypeScript编译器启用了严格模式,代码就需要对此进行检查 exports.archiveChat = functions.firestore .document('chats/{chatId}') .onUpdate( snap => {

const data=snap.after.data()
可以在没有“after”快照的情况下返回未定义的。只要您的TypeScript编译器启用了严格模式,代码就需要对此进行检查

exports.archiveChat = functions.firestore
                     .document('chats/{chatId}')
                     .onUpdate( snap => {
                        const data= snap.after.data();
                        const maxLen = 100;
                        const msgLen =  data.messages.length;
                        const charLen = JSON.stringify(data).length;
                        const batch = db.batch()

                        if( charLen >= 10000 || msgLen >= maxLen){
                            const deleteCount = msgLen - maxLen <= 0 ? 1 : msgLen - maxLen 

                          data.messages.splice(0,deleteCount);

                            const ref =  db.collection("chats").doc(snap.after.id)

                          batch.set( ref, data ,{ merge : true });
                             return  batch.commit();
                        } else{
                            return null;
                        }
                        })
const data= snap.after.data()
if (data) {
    // now it's safe to use data because it's certain to be defined
}