[英]Nextjs deployment on Vercel - api requests return 500, works on localhost
我将我的 Nextjs 应用程序部署到 Vercel 并且我的 api 调用失败并显示响应 500,即使它在 localhost 上运行也是如此。 我可以使用getStaticProps
获得初始负载,所以我相信与数据库的连接是可以的。
我的getStaticPaths
function 正常工作看起来像这样:
export async function getStaticProps() {
dbConnect();
const properties = await Property.find({
deletedAt: { $exists: false },
archivedAt: { $exists: false },
})
.sort({ refreshed: -1 })
.limit(itemsPerPage);
const propertiesCount = await Property.countDocuments(
{ deletedAt: { $exists: false }, archivedAt: { $exists: false } },
{ limit: 100 }
);
const pagesCount = Math.ceil(propertiesCount / itemsPerPage);
return {
props: {
fetchedProperties: properties.map((property) => propertyToJson(property)),
pagesCount: pagesCount,
},
revalidate: 5,
};
}
但是,当查询更改时,我会像这样调用useEffect
钩子:
useEffect(() => {
if (Object.keys(query).length > 0) {
(async () => {
const filteredProperties = await fetch(
process.env.NEXT_PUBLIC_BASE_URL +
'/api/properties?' +
new URLSearchParams(query)
);
const res = await filteredProperties.json();
if (res.status === 200) {
setProperties(res.properties);
} else {
//show error
console.log('error');
}
})();
}
}, [query]);
这总是返回错误(在本地主机上工作)。 我的pages/api/properties/index.js
文件如下所示:
import dbConnect from 'utils/dbConnect';
import Property from 'models/Property';
import { propertyToJson } from 'utils/helpers';
const handler = async (req, res) => {
console.log('req:', req);
const { method } = req;
dbConnect();
if (method === 'GET') {
const { price } = req.query;
let queryCond = {};
if (price) {
queryCond.price = { $lte: price * 1 };
}
console.log('queryCond:', queryCond);
try {
const properties = await Property.find(queryCond).exec();
console.log('properties:', properties);
res.status(200).json({
status: 200,
properties: properties,
});
} catch (err) {
res.status(500).json(err);
}
}
};
export default handler;
此外,我无法在任何地方从 api 文件中看到console.log
。 不在 Vercel function 日志中,不在我的终端中运行vercel dev --debug
。 我有一种感觉,请求甚至没有到达那个api/properties/index.js
文件......
如果您能帮助我解决这个问题,我将不胜感激,但如果您能指出如何有效地调试这些 api 文件,我将更加感激。 谢谢。
我想通了。 我的方法有两个问题:
import { adminAuth } from 'config/firebase-admin'
其中 env 变量配置不正确。 我认为这部分不应该是问题,因为我没有调用该方法的这一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.