Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
NodeJS - My method doesn't execute

inside my file ./functions/login_registration_functions.js

I have the following code

var methods = {
    check_if_not_null: function (item_to_be_checked, item_to_store_the_checked_item) {
        if(item_to_be_checked != null){
            item_to_store_the_checked_item = item_to_be_checked;
        }
}
};

module.exports = methods;

and I am calling it inside my routes.js file

var log_reg_funcs = require('./functions/login_registration_functions.js');

and I am calling the method inside my put call

log_reg_funcs.check_if_not_null(req.body.title, request.title);

but it doesn't work. when I replace the above code with below:

if(req.body.title != null){
            request.title = req.body.title;
        }

it works fine

why am I missing here?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

When you are doing this:

item_to_store_the_checked_item = item_to_be_checked;

when item_to_store_the_checked_item is the argument to your function, then you are not changing the original variable that was used when calling your function but only your local copy of the passed value that is in your function.

If you passed and object and changed one of its properties then it would work as you expect.

For example if you had:

var methods = {
    check_if_not_null: function (item_to_be_checked, object_to_store, object_key) {
        if(item_to_be_checked != null){
            object_to_store[object_key] = item_to_be_checked;
        }
    }
};

then you'd be able to do:

log_reg_funcs.check_if_not_null(req.body.title, request, 'title');

(it's strange for JavaScript to use underscores instead of camelcase but I'm keeping your style here)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda