简体   繁体   中英

Why is this code not working in NodeJS after exporting it to a separate module?

I am on my way to separate the code for my API in different modules using the exports object, as it is the most similar way to the ES6 standard (not yet supported by Node).

Here is my code currently (it can be run as shown), the problem is that, after separating, the function "cleanFormData" gets called fine, but stops without returning anything (observe the comments starting with "STACK OVERFLOW"):

File: main.js

// Dependencies:
const express = require('express');
const bodyParser = require('body-parser');

// Define app:
const app = express();

// API v0 code:
const apiV0 = require('./api0_sources/api');

// Configuration variables:
const consoleLogActions = true;

// Server start:
app.listen(8888, function () {
    console.log('Server running on port ' + this.address().port + ' - ' + new Date());
});

// For parsing every application/json request:
app.use(bodyParser.json());

// Submit data to register an user:
app.post('/registration', function (req, res) {

    res.set({'Content-Type': 'application/json'});

    // Clean the required data after obtaining it from the parsed JSON:
    let cleanedFormData = apiV0.cleanFormData({ // STACK OVERFLOW: The code stops working here.
        username: req.body.formdata.username,
        email: req.body.formdata.email,
        phone: req.body.formdata.phone,
        password: req.body.formdata.password
    });

    // The "required or not" policy is enforced here (after the strings have been cleaned to prevent blank fields to pass):
    errors = [];
    if (cleanedFormData.username === undefined)   errors.push('username_required');
    if (cleanedFormData.email === undefined)      errors.push('email_required');
    if (cleanedFormData.phone === undefined)      errors.push('phone_required');
    if (cleanedFormData.password === undefined)   errors.push('password_required');
    if (errors.length > 0) {
        let result = {
            success: false,
            errors: errors
        };

        res.jsonp(result);
    }
})
// [More things happen after]

File: ./api0_sources/api.js

// Fix and delete object's strings (for data coming from user's inputs):
exports.cleanFormData = function(object) {
    for (let i in object) {
        object[i] = String(object[i]); // Convert all the object properties to strings (to prevent problems with true, false and null).
        if ((object[i] === 'undefined') || (!object[i].replace(/\s/g, '').length)) { // Deletes 'undefined' strings, empty strings and the ones containing only spaces.
            delete object[i];
            continue; // Skip to the next loop after the property is removed.
        }
        // Do not try to fix the "password" or "newPassword" property:
        if ((i === 'password') || (i === 'newPassword')) continue;
        // Replace multiple spaces with a single one:
        object[i] = object[i].replace(/\s\s+/g, ' ');
        // Check if it is "trimmable" and if so, trim the string:
        if (object[i].trim()) object[i] = object[i].trim();
        console.log(object[i]) // Observing iterations.
    }
    if (consoleLogActions) console.log('▼ Cleaned object keys:\n', object);
    return object;
};

Before, everything was in the same file and worked perfectly fine! Can someone help me to identify what has triggered this unexpected behaviour?

UPDATED 1: Apparently, I identified the problem: I had a variable not shown in the example before: "consoleLogActions", that was only defined in the main file and apparently this stopped the function in the child module from finishing . However, absolutely no error was being thrown by Node. In the updated example it does, in my actual file it doesn't (still, no idea why).

UPDATE 2: Thanks, Marcos Casagrande. It seems like this Express middleware is catching the wrong exceptions . I had literally no idea that this could affect the rest of the code nor I have idea how to fix it. Any suggestions? :

// Detecting syntax errors (depending on the "application/type"):
app.use(function(err, req, res, next) {
    if (err instanceof SyntaxError) { // If "SyntaxError" is part of the error object:
        res
            .status(400)
            .jsonp({
                success: false,
                errors: ['bad_syntax']
            });
    }
});

Apparently, I identified the problem: I had a variable not shown in the example before: "consoleLogActions", that was only defined in the main file and apparently this stopped the function in the child module from finishing. However, absolutely no error was being thrown by Node. In the updated example it does, in my actual file it doesn't (still, no idea why).

If you're not getting any error, you probably have an express error-handling middleware, that it's not logging the error.

app.use((err, req, res, next) => {
    // console.error(err); // I'm not doing this.
    res.status(500).end();
});

Or you have an uncaughtException listener somewhere in your code.

process.on('uncaughtException', () => {}); 

The code above, will prevent uncaught errors from being logged, and the process from crashing. This is a really bad practice, and you should avoid it.

Check the following question:

Node.js Best Practice Exception Handling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM