Search code examples
node.jsexpressmulter

Send data without sending file in Node JS


I have created a Node JS app to send file along with data. It works fine when I upload a file. I want to make the uploading file an option. When I send data without file I got

TypeError: Cannot read properties of undefined(reading'filename')

I am using multer to upload file. I can download the file using res.download().

How can I fix this issue so that I can send data without the file.

This is my code:

index.js

app.post('/update',upload.single('file'), async(req,res) => {
    console.log(req.body)
    console.log(req.file)
    const a_seq_s = req.body.a_seq;
    const input_user_s = req.body.input_user;
    const input_email_s = req.body.input_email;
    const acpt_id_s = req.body.acpt_id;
    const acpt_memo_s = req.body.acpt_memo;
    const acpt_r_memo_s = req.body.acpt_r_memo;
    const file_server = req.file.filename || '';
    try{
        const query = await pool;
        const result = await query.request()
                            .input('A_SEQ',a_seq_s)
                            .input('INPUT_USER', input_user_s)
                            .input('INPUT_EMAIL',input_email_s)
                            .input('ACPT_ID',acpt_id_s)
                            .input('ACPT_MEMO',acpt_memo_s)
                            .input('ACPT_R_MEMO',acpt_r_memo_s)
                            .input('FILE_NAME',file_server || '')
                            .execute("USP_TS_M_S_Nodejs")
    }
    catch(err){
        res.status(500);
        res.send(err.message);
    }
    res.send('<script>window.close()</script>')
})

update.ejs

<form id="update" name="update" action="/update" method="post" enctype="multipart/form-data">
    <table border="1">
        <input id="a_seq" name="a_seq" value="<%=result_data[id]['A_SEQ']%>" hidden>
        <tr >
            <th scope="row">Q_name</th>
            <td>
                <input id="input_user" name="input_user" class="input" value="<%=result_data[id]['INPUT_USER']%>" >
            </td>
        </tr>
        <tr>
            <th scope="row">Q_mail</th>
            <td>
                <input id="input_email" name="input_email" class="input" value="<%=result_data[id]['INPUT_EMAIL']%>" >
            </td>
        </tr>
        <tr>
            <th scope="row">Q_ID</th>
            <td>
                <input id="acpt_id" name="acpt_id" class="input" value="<%=result_data[id]['ACPT_ID']%>">
            </td>
        </tr>
        <tr style="height: 200px;">
            <th scope="row">ask_memo</th>
            <td>
                <textarea id="acpt_memo" name="acpt_memo" class="input" value="<%=result_data[id][''][0]%>" style="height: 200px;" size="100"><%=result_data[id][''][0]%></textarea>
            </td>
        </tr>
        <tr style="height: 200px;">
            <th scope="row">memo</th>
            <td>
                <textarea id="acpt_r_memo" name="acpt_r_memo" class="input" value="<%=result_data[id][''][1]%>" style="height: 200px;" size="100"><%=result_data[id][''][1]%></textarea>
            </td>
        </tr>
        <tr>
            <th scope="row">file</th>
            <td>
                <input type="file" id="file" name="file" value="<%=result_data[id]['FILE_SERVER']%>" class="input"/>
            </td>
        </tr>
    </form>

At first, I have tried to fix this issue in update.ejs file. But then I realized may be I have to fix this in index.js.

Any help is much appreciated.


Solution

  • Currently, the code assumes that req.file will always be defined. So if there is no file, req.file.filename will cause TypeError.

    You can modify your code by checking if req.file exists or not. If exists you can read it's properties. Else you can set it as ''.

    app.post('/update', upload.single('file'), async (req, res) => {
        console.log(req.body);
        console.log(req.file);
    
        // Check if req.file exists
        const file_server = req.file ? req.file.filename : '';
    
        const a_seq_s = req.body.a_seq;
        const input_user_s = req.body.input_user;
        const input_email_s = req.body.input_email;
        const acpt_id_s = req.body.acpt_id;
        const acpt_memo_s = req.body.acpt_memo;
        const acpt_r_memo_s = req.body.acpt_r_memo;
    
        try {
            const query = await pool;
            const result = await query.request()
                .input('A_SEQ', a_seq_s)
                .input('INPUT_USER', input_user_s)
                .input('INPUT_EMAIL', input_email_s)
                .input('ACPT_ID', acpt_id_s)
                .input('ACPT_MEMO', acpt_memo_s)
                .input('ACPT_R_MEMO', acpt_r_memo_s)
                .input('FILE_NAME', file_server)
                .execute("USP_TS_M_S_Nodejs");
        } catch (err) {
            res.status(500).send(err.message);
        }
    
        res.send('<script>window.close()</script>');
    });