我的一条路线出现了这样一个奇怪的错误,但是其他路线都设置得一样,没有问题。我的路线/getThePost
抛出ReferenceError: getThePost is not defined at Object.<anonymous> (C:\Users\jakob\projects\planum-magic-functions\functions\index.js:13:26)
我尝试更改路线的名称,尝试将代码放入其他路线之一中,以查看是否出现了参考错误(不是),并且尝试将代码块...但是盯着它看,它似乎应该可以工作!
请帮助大家。
index.js
const functions = require('firebase-functions');
const app = require('express')();
const FBAuth = require('./util/fbAuth')
const { getAllPosts, createOnePost } = require('./handlers/posts');
const { login } = require('./handlers/users');
// Posts Routes
app.get('/posts', getAllPosts);
app.get('/post/:postId', getThePost);
app.post("/post", FBAuth, createOnePost);
//TODO delete post
//TODO update post
// Login Route
app.post('/login', login)
exports.api = functions.https.onRequest(app)
posts.js
const { db } = require('../util/admin');
exports.getAllPosts = (req, res) => {
db.collection("posts")
.orderBy("createdAt", "desc")
.get()
.then(data => {
let posts = [];
data.forEach(doc => {
posts.push({
postId: doc.id,
name: doc.data().name,
images: doc.data().images,
link: doc.data().link,
info: doc.data().info,
price: doc.data().price,
itemCategory: doc.data().itemCategory,
available: doc.data().available,
highEnd: doc.data().highEnd,
createdAt: doc.data().createdAt
});
});
return res.json(posts);
})
.catch(err => console.error(err));
};
exports.getThePost = (req, res) => {
let postData = {};
db.doc(`/posts/${req.params.postId}`)
.get()
.then(doc => {
if (!doc.exists) {
return res.status(404).json({ error: "Post not Found" });
}
postData = doc.data();
// postData.postId = doc.id;
return res.json(postData);
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: err.code });
});
};
exports.createOnePost = (req, res) => {
const newPost = {
name: req.body.name,
images: req.body.images,
link: req.body.link,
info: req.body.info,
price: req.body.price,
itemCategory: req.body.itemCategory,
available: req.body.available,
highEnd: req.body.highEnd,
createdAt: new Date().toISOString()
};
db.collection("posts")
.add(newPost)
.then(doc => {
res.json({ message: `document ${doc.id} created successfully` });
})
.catch(err => {
res.status(500).json({ error: "something went wrong" });
console.error(err);
});
};
答案 0 :(得分:0)
您忘记了在此处导入getThePost
方法
const { getAllPosts, createOnePost, getThePost } = require('./handlers/posts');