Node.js 没有与提供的标识符对应的用户记录
此函数向x天前安装应用程序的用户发送通知,检索创建其帐户的日期,并将其fcm令牌保存在数据库中。由于该函数尚未完全运行,因此我此时不使用用户令牌是正常的。我有一个错误:Node.js 没有与提供的标识符对应的用户记录,node.js,firebase,google-cloud-functions,Node.js,Firebase,Google Cloud Functions,此函数向x天前安装应用程序的用户发送通知,检索创建其帐户的日期,并将其fcm令牌保存在数据库中。由于该函数尚未完全运行,因此我此时不使用用户令牌是正常的。我有一个错误: Error: There is no user record corresponding to the provided identifier. at FirebaseAuthError.Error (native) at FirebaseAuthError.FirebaseError [as construct
Error: There is no user record corresponding to the provided identifier.
at FirebaseAuthError.Error (native)
at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:113:15
at /user_code/node_modules/firebase-admin/lib/auth/auth-api-request.js:332:13
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
我找不到它的来源。正如你所看到的,我尝试了很多方法来捕捉错误,但是没有任何效果
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotificationToNewUser = functions.https.onRequest((request, response) => {
Date.prototype.sameDay = function (d) {
return this.getFullYear() === d.getFullYear()
&& this.getDate() === d.getDate()
&& this.getMonth() === d.getMonth();
}
// Loop through users in order with the forEach() method. The callback
// provided to forEach() will be called synchronously with a DataSnapshot
// for each child:
var query = admin.database().ref("users").orderByKey();
var defaultAuth = admin.auth();
const getUsersPromise = query.once("value")
return Promise.all([getUsersPromise]).then(results => {
const tokensSnapshot = "test";
results[0].forEach(function (childSnapshot) {
if (childSnapshot != null) {
try {
// user will be "ada" the first time and "alan" the second time
var user_id = childSnapshot.key;
// childData will be the actual contents of the child
var user_data = childSnapshot.val();
admin.auth().getUser(user_id)
.then(function (userRecord) {
if (userRecord != null) {
var create_date = userRecord.metadata.createdAt
var date_now = new Date(Date.now());
console.log("Creation date:", create_date);
create_date.setDate(create_date.getDate() + 4);
if (create_date.sameDay(date_now)) {
//todo something here
}
}
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log('User did not sign up correctly');
console.log(errorCode);
console.console.log(errorMessage);
});
}
catch (e) {
console.log(e);
}
}
});
// Notification details.
const payload = {
notification: {
body: "blabla",
sound: "default"
},
"data": {
"key": "CODE",
"value": "playstore"
}
};
var options = {
priority: "high",
collapseKey: "playstore"
};
// Listing all tokens, error here below.
//const tokens = Object.keys(tokensSnapshot.val());
const tokens = "my token for the test";
// Send notifications to all tokens.
// Send a message to the device corresponding to the provided
// registration token with the provided options.
return admin.messaging().sendToDevice(tokens, payload, options).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
else {
console.log("Successfully sent message:", response);
}
});
return Promise.all(tokensToRemove);
})
})
})
我的数据库:
root
--->users
------->user1 (id provided by auth)
------------>fcm_tokens
------------>id provided by auth
------->user2
------------>fcm_tokens
------------>id provided by auth
你能将
/users
端点的示例粘贴到数据库中吗?@Chris done,看看端点你为什么不使用函数。auth.user().onCreate()
云函数?在您的情况下,似乎更容易和更干净。我没有使用此功能,因为我想在用户创建其帐户5天后发送通知。我还没有找到一种在创建帐户后立即启动此函数的方法…当我使用admin.auth().getUserByEmail()
和admin.auth().createUser()
时,我也看到了一个类似的错误,但我不知道是哪个调用导致了它。奇怪的是,我正在进行的所有api调用都是使用.catch()
处理的,但它们似乎没有抓住这个问题。