首页 > 解决方案 > 意外的令牌 < 在 JSON 中的位置 0 用 firebase auth 表示

问题描述

我使用firebase auth在express中制作了一个身份验证系统。我在 express 中创建了一个端点,如下所示:

app.post('/users/login', (req, res) => {
    console.log('logare...');
    firebase.auth().signInWithEmailAndPassword(req.body.email, req.body.parola)
        .then(user => {
            res.json({ succes: true, msg: "te-ai autentificat cu succes" });
        })
        .catch(err => {
            console.log('date invalide');
            res.json({ succes: false, msg: 'date invalide', errCode: err.code });
        })
})

然后我在我的字体端 js 文件中创建了一个函数,用于向服务器发送请求:

const autentificare = async (email, parola) => {
    return await fetch('https://cm-api-1.herokuapp.com/users/login', {
        method: 'POST',
        headers: {
            'content-type': "application/json"
        },
        body: JSON.stringify({
            email: email,
            pasword: parola
        })
    })
        .then(res => {
            console.log(res.status)
            return res.json();
        });
};

在我的 app.js 文件中,我为 verfy 电子邮件和密码创建了提交事件:

form.addEventListener('submit', async e => {
    e.preventDefault();
    const email = emailDom.value;
    const parola = parolaDom.value;
    await autentificare(email, parola)
        .then(data => {
            if (data.succes) {
                console.log('ok datele sunt valide')
            } else {
                console.log('date invalide')
            }
        })
});

当我在控制台中提交表单时,它向我显示了这个错误:我POST https://cm-api-1.herokuapp.com/users/login 500 (Internal Server Error)Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0api 它部署在 heroku 上。我不知道我错过了什么

标签: javascripthtmlfirebaseexpress

解决方案


根据我编写 REST API 的经验,此错误通常发生在 API 本身存在语法错误或异常时。代码看起来不错,但 HTML 错误 500 表明它不在客户端。

我没有使用 firebase 和 express 的经验,我主要使用 Swagger 和 php 编写 REST API。


推荐阅读