使用GET定义相同的常量,而使用POST定义未定义的常量-express,mongo,react

我正在关注一个React教程,其中我正在MERN堆栈中构建一个基本博客。

In this particular case, in my Server.js file I am attempting a POST request to MongoDB. It identifies the collection & JSON object using URL parameters, alters the object & returns a new constant equal to the new object.

The issue is that the const equal to the object found is showing to be not defined. However, I cannot figure out why that would be. Another function using a GET request with the same const (each are local to their respective functions) works as designed with no error regarding defining the const.

import express from 'express';
import bodyParser from 'body-parser';
import { MongoClient } from 'mongodb';

const app = express();

// also tried to use express' built in bodyparser, same error
app.use(bodyParser.json());

// working GET req function using the same const
app.get('/api/articles/:name', async (req,res) => {
    try {
        const articleName = req.params.name;

        const client = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true });
        const db = client.db('my-app-db');

// this is the const in question, equal to object found
// based on name param passed in URL
        const articleInfo = await db.collection('articles').findOne({ name: articleName });
        res.status(200).json(articleInfo);

        console.log(articleInfo);

        client.close();
    } catch (error) {
        res.status(500).json({message: 'Error connecting to db', error});
    }
    
});

// non working POST req function, using the same const
app.post('api/articles/:name/upvote', async (req,res) => {
    try {
        const articleName = req.params.name;

        const client = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true });
        const db = client.db('my-app-db');
        
        // gets error for being undefined at Server.js 32:5
        const articleInfo = await db.collection('articles').findOne({ name: articleName });
        await db.collection('articles').updateOne({ name: articleName },{
            '$set' : {
                upvotes: articleInfo.upvotes + 1,
            }, 
        });
        
        // this is updated article sent as response 
        const updatedArticleInfo = await db.collection('articles').findOne({ name: articleName });

        res.status(200).json(updatedArticleInfo);
        client.close();

    } catch (error) {
        res.status(500).json({message: 'Error connecting to db', error});
    }
})

app.listen(8000, () => console.log('Listening on port 8000'));

这是与邮递员收到的错误完全相同的错误

ReferenceError:未定义articleInfo      在C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ src / Server.js:32:5      在Layer.handle上[作为handle_request](C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ layer.js:95:5)      在下一个(C:\ Users \ oduboise \ Desktop \ Sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ route.js:137:13)      在Route.dispatch(C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ route.js:112:3)      在Layer.handle上[作为handle_request](C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ layer.js:95:5)      在C:\ Users \ oduboise \ Desktop \ Sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ index.js:281:22      在参数下(C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ index.js:354:14)      在参数上(C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ index.js:365:14)      在Function.process_params(C:\ Users \ oduboise \ Desktop \ sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ index.js:410:3)      在下一个(C:\ Users \ oduboise \ Desktop \ Sandbox \ react-full-stack \ my-app-backend \ node_modules \ express \ lib \ router \ index.js:275:10)

This is the response from the successful GET request, as a JSON object

{
"_id": "5ec60f1390d2ce923cdb0e41",
"name": "what-react",
"upvotes": 0,
"comments": []
}

这也是我第一次在StackOverflow中提出问题,通常我能够弄清楚/找到它。因此,任何问题格式提示也将不胜感激