Cloud Function - 在 onCreate(user) 中两次将数据插入 admin.firestore 无效
Cloud Function - Inserting data to admin.firestore two times in onCreate(user) is not working
我正在使用云功能在两个不同的文档中插入 FireStore 中的用户数据,phoneNumber/email (userInfoRef)
的第一个插入工作正常,而 uid (searchRef)
的插入不工作。
此外,云函数日志 return 没有任何错误
DEBUG: Billing account not configured..
INFO: Function execution started
INFO: Billing account not configured.. //again
DEBUG: new user is here //(my written log)
DEBUG: null//(my written log)
DEBUG: Function execution took 2892 ms, finished with status: 'ok'
这是我的代码
import * as functions from 'firebase-functions';
exports.saveUserDataToDatabase = functions.auth.user().onCreate((user) => {
console.log('new user is here');
console.log(user.photoURL);
const email = user.email;
const phoneNumber = user.phoneNumber;
const uid = user.uid;
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
const settings = {timestampsInSnapshots: true};
db.settings(settings);
const userInfoRef = db.collection("users");
if (email === null){
userInfoRef.doc(uid).set({
"phone": phoneNumber
});
const searchRef = db.collection("Search_PhoneByUid");
searchRef.doc(phoneNumber).set({
"uid": uid
});
return true;
} else if (phoneNumber === null) {
userInfoRef.doc(uid).set({
"email": email
});
const searchRef = db.collection("Search_EmailByUid");
searchRef.doc(email).set({
"uid": uid
});
return true;
}
return false;
});
您没有return承诺在您的函数中完成所有异步工作后就解决了。您忽略了对 set()
的所有调用 return 的所有承诺。如果您没有 return 承诺,Cloud Functions 可能会在异步工作完成之前关闭您的函数。
有关详细信息,请参阅 documentation。
我正在使用云功能在两个不同的文档中插入 FireStore 中的用户数据,phoneNumber/email (userInfoRef)
的第一个插入工作正常,而 uid (searchRef)
的插入不工作。
此外,云函数日志 return 没有任何错误
DEBUG: Billing account not configured..
INFO: Function execution started
INFO: Billing account not configured.. //again
DEBUG: new user is here //(my written log)
DEBUG: null//(my written log)
DEBUG: Function execution took 2892 ms, finished with status: 'ok'
这是我的代码
import * as functions from 'firebase-functions';
exports.saveUserDataToDatabase = functions.auth.user().onCreate((user) => {
console.log('new user is here');
console.log(user.photoURL);
const email = user.email;
const phoneNumber = user.phoneNumber;
const uid = user.uid;
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
const settings = {timestampsInSnapshots: true};
db.settings(settings);
const userInfoRef = db.collection("users");
if (email === null){
userInfoRef.doc(uid).set({
"phone": phoneNumber
});
const searchRef = db.collection("Search_PhoneByUid");
searchRef.doc(phoneNumber).set({
"uid": uid
});
return true;
} else if (phoneNumber === null) {
userInfoRef.doc(uid).set({
"email": email
});
const searchRef = db.collection("Search_EmailByUid");
searchRef.doc(email).set({
"uid": uid
});
return true;
}
return false;
});
您没有return承诺在您的函数中完成所有异步工作后就解决了。您忽略了对 set()
的所有调用 return 的所有承诺。如果您没有 return 承诺,Cloud Functions 可能会在异步工作完成之前关闭您的函数。
有关详细信息,请参阅 documentation。