最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase.auth().createUserWithEmailAndPassword Undefined is not a function - Stack Overflow

programmeradmin1浏览0评论

I am following the firebase documentation on user management

var firebase = require('firebase');

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

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);
    });
});

I get the following error

undefined is not a function

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

From doing a little bit of research, I think undefined is not a function is basically a javascript version of a null pointer exception (could be wrong)

I have configured my service account and project on firebase. I am running this on localhost. Thanks. I am looking primarily for debugging tips, as I'm not sure how to test what isn't working.

I am following the firebase documentation on user management

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);
    });
});

I get the following error

undefined is not a function

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

From doing a little bit of research, I think undefined is not a function is basically a javascript version of a null pointer exception (could be wrong)

I have configured my service account and project on firebase. I am running this on localhost. Thanks. I am looking primarily for debugging tips, as I'm not sure how to test what isn't working.

Share Improve this question asked May 28, 2016 at 22:52 Script KittyScript Kitty 1,7475 gold badges28 silver badges47 bronze badges 6
  • Same as stackoverflow.com/questions/37485998/… (has no answer yet). – Frank van Puffelen Commented May 28, 2016 at 23:07
  • @FrankvanPuffelen Apologies, did not see that question yet. – Script Kitty Commented May 28, 2016 at 23:19
  • I have found a work-around for this using anonymous user authentication from the client side. Kindly elaborate your use case, so that I could write an answer based on that. – Niwin Santhosh Commented Jul 16, 2016 at 14:26
  • @NiwinSanthosh Hi, thanks for your input. What ended up happening was I dumped nodejs server side code entirely to structure my web app and use firebase static hosting. Using static web auth was easier for me. Since asking this, however, I've been looking back into server side because I want to send custom emails but firebase has deprecated this functionality :( . – Script Kitty Commented Jul 16, 2016 at 20:26
  • 1 stackoverflow.com/questions/38813062/… – JRam Commented Sep 20, 2016 at 14:11
 |  Show 1 more comment

5 Answers 5

Reset to default 6

If you haven't fixed the problem yet, I found out why it's giving such an error and that's because it really isn't a function.

You're using the Web reference as your guide when you need to be using the Server reference: https://firebase.google.com/docs/reference/node/firebase.auth.Auth.

As you can see, firebase.auth.Auth is a lot different on the Node.js API when compared to the Web API as there's not a lot in there. Now you have to deal with tokens, which I haven't looked deep into, if using the Server API. On the other hand, you can just use the Web API, which does things you intended to do.

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.

I am not sure this is correct, but it helps:

  1. In console.firebase.google.com get script:

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

and Config:

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

  1. Copy firebase.js file from

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

to local project fb/firebase.js

  1. In firebas-service.ts:

    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{
            // ...
        }
    });
}

Could this be because you defined the error as "err", but under errorMessage, "error.message" was the code? Not quite sure, but try this and see if it fixes:

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);
    });
});

Question is old and I didn't see any accepted answer. And for the other people who still face the same problem. This is what I did to resolve my issue.

  • Remove the node_modules
  • Remove the firebase from package.json and install the latest firebase npm install --save firebase
  • And run npm install once again so that all other dependencies also get installed

After that add

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

Whatever module you want to use in your project, you can mentioned accordingly. Then initialise the firebase

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);

Later you can write your API to create user with email and password

app.post('/register', function(req, res) {
    var emailId = req.email
    var password = req.password;
    firebase.auth().createUserWithEmailAndPassword(emailId, password).catch(function(error) {
      // Handle Errors here.
      console.log(error)
    });

})
发布评论

评论列表(0)

  1. 暂无评论