firebase.auth()。createUserWithEmailAndPassword Undefined不是函数

时间:2016-05-28 22:52:11

标签: javascript node.js express firebase firebase-authentication

我正在关注有关用户管理的firebase文档

var firebase = require('firebase');

// Initialize Firebase
firebase.initializeApp({
    serviceAccount: "./<mysecreturledittedout>.json",
    databaseURL: "https://<mydatabaseurljusttobesafe>.firebaseio.com"
});

router.get('/create', function(req, res){
    var email = req.email;
    var password = req.password;
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(err){ // LINE 27 CRASH
        var errorCode = err.code;
        var errorMessage = error.message;
        console.log("ERROR");
        console.log(errorCode, errorMessage);
    });
});

我收到以下错误

undefined is not a function

TypeError: undefined is not a function
    at d:\Users\Kitty\Code\Practice2\routes\index.js:27:21

从做一点研究,我认为undefined不是一个函数,基本上是一个空指针异常的javascript版本(可能是错误的)

我已经在firebase上配置了我的服务帐户和项目。我在localhost上运行它。谢谢。我主要关注调试技巧,因为我不确定如何测试什么不起作用。

5 个答案:

答案 0 :(得分:6)

如果你还没有解决问题,我发现了它为什么会出现这样的错误,因为它确实不是一个功能。

当您需要使用服务器参考时,您可以使用Web参考作为指南:https://firebase.google.com/docs/reference/node/firebase.auth.Auth

正如您所看到的,与Web API相比,node.js API上的firebase.auth.Auth有很多不同,因为那里并不是很多。现在,如果使用Server API,您必须处理令人深入研究的令牌。另一方面,您可以使用Web API,它可以执行您打算执行的操作。

答案 1 :(得分:3)

Update
Sometime at or before firebase 3.4.1 for Node.js, createUserWithEmailAndPassword became available.

Original
As of firebase 3.1.0 for Node.js, createUserWithEmailAndPassword is not yet supported. According to a response on the firebase Google group, not all platforms have feature parity in the 3.x libraries yet. The REST interface does support user creation but is clearly a different approach than using the Node.js library.

Go to https://groups.google.com/forum/#!forum/firebase-talk and search for createUserWithEmailAndPassword for the latest on that discussion.

答案 2 :(得分:1)

我不确定这是否正确,但它有所帮助:

  1. 在console.firebase.google.com中获取 脚本:
  2. &#13;
    &#13;
    <script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
    &#13;
    &#13;
    &#13;

    和配置:

    &#13;
    &#13;
    var config = {
        apiKey: "xxxxxxxxxxxxxxx",
        authDomain: "project-nnnnnnnnnnn.firebaseapp.com",
        databaseURL: "https://project-nnnnnnnnnnn.firebaseio.com",
        storageBucket: "project-nnnnnnnnnnn.appspot.com",
    };
    &#13;
    &#13;
    &#13;

    1. 复制firebase.js文件

      &#13;
      &#13;
      <script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
      &#13;
      &#13;
      &#13;

      到本地项目fb / firebase.js

      1. 在firebas-service.ts中:
      2. &#13;
        &#13;
            var firebase = require('./fb/firebase');
        // Initialize Firebase
        var config = {
            apiKey: "xxxxxxxxxxxxxxx",
            authDomain: "project-nnnnnnnnnnn.firebaseapp.com",
            databaseURL: "https://project-nnnnnnnnnnn.firebaseio.com",
            storageBucket: "project-nnnnnnnnnnn.appspot.com",
        };
        firebase.initializeApp(config);
        
        export function createUser (email : string, password:string){
            firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
                if (error.code){
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    alert(errorMessage);
                    console.log(errorMessage);
                }
                else{
                    // ...
                }
            });
        }
        &#13;
        &#13;
        &#13;

答案 3 :(得分:0)

这可能是因为您将错误定义为&#34;错误&#34;,但在errorMessage下,&#34; error.message&#34;是代码?不太确定,但尝试这个,看看它是否修复:

var firebase = require('firebase');

// Initialize Firebase
firebase.initializeApp({
    serviceAccount: "./<mysecreturledittedout>.json",
    databaseURL: "https://<mydatabaseurljusttobesafe>.firebaseio.com"
});

router.get('/create', function(req, res){
    var email = req.email;
    var password = req.password;
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error){ 
        var errorCode = error.code;
        var errorMessage = error.message;
        console.log("ERROR");
        console.log(errorCode, errorMessage);
    });
});

答案 4 :(得分:0)

问题很老,我没有看到任何可接受的答案。对于仍然面临同样问题的其他人。这就是我解决问题的方法。

  • 删除node_modules
  • 从package.json中删除firebase,然后 安装最新的Firebase jar
  • 并再次运行npm install,以便所有其他依赖项也得到 已安装

之后添加

npm install --save firebase

无论您想在项目中使用什么模块,都可以相应提及。然后初始化firebase

var firebase = require("firebase/app");
require("firebase/firestore");
require("firebase/auth");

稍后您可以编写API来使用电子邮件和密码创建用户

const firebaseConfig = {
    apiKey: "XXXXXXXXXXXXXXXXXXXXXXX",
    authDomain: "XXXXXXXXXXXXXXX",
    databaseURL: "https://XXXXXX.COM",
    projectId: "XXXXXXXXX",
    storageBucket: "XXXXXXXXX",
    messagingSenderId: "XXXXXXXX",
    appId: "XXXXXXXXXXXXX",
    measurementId: "XXXXXXX"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);