当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Sails.js model acces by string name

问题描述:

Is there any way to access models in sails by its name being contained in a string?

for example, if I want to create a user, instead of doing

User.create({name: 'martin'});

I need to do something like

sails['User'].create({name: 'martin'});

网友答案:

All references to models are stored in globally accessible object sails.models. You can access any of your model using the array literal notation [].

var model = sails.models['users'];
model.create({name: 'martin'});

The same rule applies to controllers and services.

分享给朋友:
您可能感兴趣的文章:
随机阅读: