Javascript TypeError:。拼接云函数
我正试图用javascript为Google firestore编写一个云函数。它需要循环使用注册标记并删除不必要的标记。我想我已经记下了大部分代码,但出于某种原因,它不喜欢我在数组上调用splice,我不是javascript开发人员,这让我感到困惑,我将在这里发布整个脚本,因为它不是很大,欢迎任何帮助Javascript TypeError:。拼接云函数,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我正试图用javascript为Google firestore编写一个云函数。它需要循环使用注册标记并删除不必要的标记。我想我已经记下了大部分代码,但出于某种原因,它不喜欢我在数组上调用splice,我不是javascript开发人员,这让我感到困惑,我将在这里发布整个脚本,因为它不是很大,欢迎任何帮助 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.notifyNewMessage = functions.firestore.document
('users/{ID}/contacts/{contactID}/messages/{messageID}')
.onCreate((docSnapshot , context) => {
const message = docSnapshot.data()
const recipientId = message['recipientId']
const senderId = message['sender']
const senderName = message['senderName']
return admin.firestore().doc('users/' + recipientId +
"/token/token").get().then(userDoc =>{
const registrationTokens = userDoc.get('token')
const notificationBody = (message['data_type'] === "TEXT") ? message['message'] : "You received a new mage message."
const payload = {
notification : {
title : senderName + "sent you a message.",
body : notificationBody,
clickAction : "MessageListActivity"
},
data : {
USER_NAME : senderName,
USER_NUMBER : message['sender']
}
}
return admin.messaging().sendToDevice(registrationTokens,payload).then(response => {
const stillRegisteredTokens = registrationTokens
response.results.forEach((result , index) => {
const error = result.error
if(error){
const failedRegistrationToken = registrationTokens[index]
console.error('failed token ', failedRegistrationToken, error)
if(error.code === 'messaging/invalid-registration-token'
|| error.code === 'messaging/registration-token-not-registered')
{ const failedIndex = stillRegisteredTokens.indexOf(failedRegistrationToken)
if(failedIndex > -1){
stillRegisteredTokens.splice(failedIndex, 1)
}
}
}
})
return admin.firestore().doc("users" + recipientId + "token" + "token").update({
registrationTokens : stillRegisteredTokens
})
})
})
})
我最好的猜测是,
stillRegisteredTokens
不是数组。也就是说,const registrationTokens=userDoc.get('token')
不返回数组。您可能需要console.log(JSON.stringify(registrationTokens))
查看它的实际类型。多谢您的好意,我会检查一下它是什么,然后发回