0

i am new in node js. Please helm me out. In the following code "modelName" is a variable which may be changed in each function call but it is showing me error that "modelName" is undefined. Thanks

"createModel": function(modelName, callback) {
    app.models.modelName.create(self.modelName(), function(error, modelInstance) {
        if(!error){
            callback(null, modelInstance.id);
        }
    });
}
GG.
  • 21,083
  • 14
  • 84
  • 130

2 Answers2

0

I'm not sure what you're asking. Are you referring to the app.models.modelName or the self.modelName() reference? Either way, those are completely different references than the modelName parameter you are defining in the function signature (modelName, callback).

If I read your question correctly, you are trying to access a property dynamically based on the modelName that is passed in. If so, you can't use dot notation, try something like this:

"createModel": function(modelName, callback) {
    app.models[modelName].create(self[modelName](), function (error, modelInstance) {
        if(!error){
            callback(null, modelInstance.id);
        }
    });
}
Michael Payne
  • 534
  • 5
  • 13
0
"createModel": function(modelName, callback) {
    app.models[modelName].create(self.modelName(), function(error, modelInstance) {
        if(!error){
            callback(null, modelInstance.id);
        }
    });
}

You need to access dynamic properties with bracket notation instead of dot notation.

bcr
  • 3,791
  • 18
  • 28