Search code examples
javascriptloopbackjsloopback

Calling a function in a loopback model.js file from a regular JS file


I am trying to call a function in loopback model.js file called models/customerProfile.js from a JS file server/script/myScripts.js but I am getting this error:

Unhandled error for request GET /api/CustomerProfiles/verifyCustomer?id=38733: TypeError: myScripts.verifyCustomerProfile is not a function

I want to keep all my generic functions in this myScripts file and call them from my model.js files.

I did the following in my customerProfile.js model file to include the script:

var myScripts = require('../../server/script/myScripts');

then in my code I referenced it as follows:

myScripts.functionName();

It doesn't work.


Solution

  • Did you export your functions ?

    myScripts.js

    Try this one :

    module.exports = {
    
    functionName : function() {
     // Your code
    },
    
    functionName2 : function() {
     // some code
    },
    
    // You can add more functions
    
    }