const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase = require('firebase/app');
const credentials = require('./credentials.json');
const db = admin.firestore();
const user = require('./http-functions/user-creation')
var config = {
apiKey: "KEY",
authDomain: "ecommerce.firebaseapp.com",
databaseURL: "https://ecommerce.firebaseio.com",
projectId: "ecommerce",
storageBucket: "ecommerce.appspot.com",
messagingSenderId: "ID"
};
firebase.initializeApp(config)
admin.initializeApp({
credential: admin.credential.cert(credentials),
databaseURL: "https://ecommerce.firebaseio.com",
projectId: "ecommerce"
});
exports.addUser = functions.https.onRequest((req, res) => user.addUser(req, res, db, firebase))
我正在导入firebase,以便可以使用firebase.auth()在云函数firestore上创建一个新用户。但是我在运行Firebase部署时显示以下错误:
错误:默认的Firebase应用程序不存在。使用任何Firebase服务之前,请确保先调用initializeApp()。 在FirebaseAppError.FirebaseError [作为构造函数]
下面是添加新用户的代码
module.exports = {
addUser: function(req, res, db, firebase, admin) {
firebase.auth().createUserWithEmailandPassword(email, password)
.then(userRecord => {
const uid = userRecord.uid;
const email = userRecord.email;
return firebase.firestore()
.collection('users')
.doc(uid)
.set({email: email},
{merge: true});
})
.catch(err => {
console.log(err)
})
}
}
答案 0 :(得分:0)
尝试将True
分配给const并使用它代替intitializeApp
类似的东西:
firebase.auth()