所以我在學習 Firebase / React 教程時遇到了一個問題,我剛剛完成了從這里開始的重構部分:https : //www.youtube.com/watch?v= m_u6P5k0vP0&t=5590s
一切都很順利,除了我發布一個新的 Twitter 風格更新的帖子功能。嘗試運行我得到的firebase服務器時出現的錯誤是:
? Error: Route.post() requires a callback function but got a [object Undefined]
at Route.<computed> [as post] (/Users/tmac/Programing/HolbertonFinal/MentorMatchingApp/firebase-fuctions/functions/node_modules/express/lib/router/route.js:202:15)
at Function.app.<computed> [as post] (/Users/tmac/Programing/HolbertonFinal/MentorMatchingApp/firebase-fuctions/functions/node_modules/express/lib/application.js:482:19)
at Object.<anonymous> (/Users/tmac/Programing/HolbertonFinal/MentorMatchingApp/firebase-fuctions/functions/index.js:10:5)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at initializeRuntime (/Users/tmac/.nvm/versions/node/v17.3.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:655:29)
? We were unable to load your functions code. (see above)
我會嘗試在這里獲取相關模塊,但如果我遺漏了什么,請告訴我,我可以添加它。我對此很陌生,非常感謝所有幫助!
index.js
const functions = require("firebase-functions");
const app = require('express')();
const { postOneScream } = require('./handlers/screams'); // Not Working
const { signup, login } = require('./handlers/users');
const { FBAuth } = require('./util/fbAuth');
// Scream routes - Testing post functionality for social media feed posts
app.post('/screams', FBAuth, postOneScream); // Error: Route.post() requires a callback function but got a [object Undefined]
// Signup route
app.post("/signup", signup);
// Sign In route
app.post('/login', login);
// export api allows us to use express for our function formating
exports.api = functions.https.onRequest(app);
fbAuth.js
const { admin } = require('./admin');
// Check if user has a token for being logged in
module.exports = (req, res, next) => {
let idToken;
if(req.headers.authorization && req.headers.authorization.startsWith('Bearer ')){
idToken = req.headers.authorization.split('Bearer ')[1];
} else {
console.error('No token found');
return res.status(403).json({error: 'Unauthorized'});
}
admin.auth().verifyIdToken(idToken)
.then(decodedToken => {
req.user = decodedToken;
console.log(decodedToken);
return db.collection('users')
.where('userId', '==', req.user.uid)
.limit(1)
.get();
})
.then(data => {
req.user.handle = data.docs[0].data().handle;
return next();
})
.catch((err) => {
console.error('Error while verifying token', err);
return res.status(403).json({err})
})
}
尖叫.js
const { db } = require('../util/admin');
exports.postOneScream = (req, res) => {
const newScream = {
body: req.body.body,
userHandle: req.user.handle,
createdAt: new Date().toISOString()
};
db.collection('screams').add(newScream).then((doc) => {
res.json({ message: `document ${doc.id} created successfully` })
})
.catch((err) => {
res.status(500).json({ error: 'something went wrong' });
console.error(err);
})
};
再次感謝您提供的所有幫助,我真的很想學習 webdev,但是男孩有很多,每一步只會讓我更多地了解我不知道的程度:)
uj5u.com熱心網友回復:
因此,在 index.js 中,您正在從“./util/fbAuth”匯入“FBAuth”,
const { FBAuth } = require("./util/fbAuth");
但在“./util/fbAuth”中,您沒有匯出“FBAuth”。
將您的 fbAuth 檔案修改為這樣的內容,
const { admin } = require('./admin');
// Check if user has a token for being logged in
module.exports.fbAuth = (req, res, next) => {
let idToken;
if(req.headers.authorization && req.headers.authorization.startsWith('Bearer ')){
idToken = req.headers.authorization.split('Bearer ')[1];
} else {
console.error('No token found');
return res.status(403).json({error: 'Unauthorized'});
}
admin.auth().verifyIdToken(idToken)
.then(decodedToken => {
req.user = decodedToken;
console.log(decodedToken);
return db.collection('users')
.where('userId', '==', req.user.uid)
.limit(1)
.get();
})
.then(data => {
req.user.handle = data.docs[0].data().handle;
return next();
})
.catch((err) => {
console.error('Error while verifying token', err);
return res.status(403).json({err})
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/420633.html
標籤:
