remove console.logs, POST only for updateReceipt

This commit is contained in:
Devin Deng 2022-11-06 16:53:32 -08:00 committed by Michael Zhang
parent 210f84a0b3
commit 4a957bce10
3 changed files with 12 additions and 8 deletions

View file

@ -1,17 +1,22 @@
import { MongoClient } from 'mongodb'; import { Db, MongoClient } from 'mongodb';
const USERNAME = process.env.MONGO_USERNAME const USERNAME = process.env.MONGO_USERNAME
const PASSWORD = process.env.MONGO_PASSWORD const PASSWORD = process.env.MONGO_PASSWORD
const HOSTNAME = process.env.MONGO_HOSTNAME const HOSTNAME = process.env.MONGO_HOSTNAME
const DATABASENAME = process.env.MONGO_DATABASE_NAME
const URI = const URI =
`mongodb://${USERNAME}:${PASSWORD}@${HOSTNAME ?? 'localhost'}:3001`; `mongodb://${USERNAME}:${PASSWORD}@${HOSTNAME ?? 'localhost'}:3001`;
let db: Db | null = null;
export const getMongoDBClient = async () => { export const getMongoDBClient = async () => {
if (db) {
return db;
}
const client = new MongoClient(URI); const client = new MongoClient(URI);
await client.connect(); await client.connect();
const receiptdb = client.db("receipt"); return client.db(DATABASENAME);
return receiptdb.collection('receipt');
} }

View file

@ -6,11 +6,12 @@ export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<Record<string, string>[] | Record<string, string>> res: NextApiResponse<Record<string, string>[] | Record<string, string>>
) { ) {
if (req.method !== 'POST') {
return res.status(405).json({message: 'Only POST method allowed'});
}
const receipt = req.body; const receipt = req.body;
const client = await getMongoDBClient(); const client = await getMongoDBClient();
console.log('called', receipt);
try { try {
// TODO implement this // TODO implement this
res.status(200).json(receipt); res.status(200).json(receipt);

View file

@ -58,8 +58,6 @@ const Home: NextPage = () => {
}; };
const receiptJSONString = JSON.stringify(receiptJson); const receiptJSONString = JSON.stringify(receiptJson);
console.log("ASDFASDF", receiptJSONString);
useEffect(() => { useEffect(() => {
const updateDb = async () => { const updateDb = async () => {
const response = await fetch("/api/createReceipt", { const response = await fetch("/api/createReceipt", {