javascript - 意外的令牌 < 在 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 0
api 它部署在 heroku 上。我不知道我错过了什么
解决方案
根据我编写 REST API 的经验,此错误通常发生在 API 本身存在语法错误或异常时。代码看起来不错,但 HTML 错误 500 表明它不在客户端。
我没有使用 firebase 和 express 的经验,我主要使用 Swagger 和 php 编写 REST API。
推荐阅读
- firebase - Flutter Firestore 插件在 Xcode 中存档时抛出错误
- javascript - 用javascript交换用作背景的图像
- python - PySpark:如何根据一列中的值复制数据框的行
- google-places-api - 在哪里可以找到所有可用 address_component 值的列表?
- java - Spark 1.6 与 Kryo 2.21 和 ArrayList
- javascript - 如何针对不同的条件将日期设置为 ISOString
- python - Wireshark中的Python UDP数据包不一样
- java - 使用自定义转换器推土机映射继承的类
- rhel - systemctl 因未知部分“计时器”而失败
- python - 在 tensorflow 中组合计算图