Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!