如何获取Strongloop Loopback模型?

13

这真是让人崩溃,我该如何获取一个回环模型以便以编程方式处理它?我有一个名为“Notification”的持久化模型,我可以使用REST浏览器与其交互。我希望能够在服务器内部处理它,即Notification.find(...)。我执行了app.models()并看到它列出。我已经完成以下操作:

var Notification = app.models.Notification;

然后得到一个又大又胖的“未定义(undefined)”错误。我已经这样做了:

var Notification = loopback.Notification;
app.model(Notification);
var Notification = app.models.Notification;

还有一个大胖的 "undefined"。

请解释一下我需要做什么才能获取我使用以下方式定义的模型:

slc loopback:model

提前感谢


请查看 https://groups.google.com/forum/#!topic/loopbackjs/Z5VNL5Aw4Cs。 - Raymond Feng
可能对某些人有用:如果在“初始化”模型之前访问它,则会返回未定义。只需尝试从放置在server/boot中的脚本中访问模型即可。这里有一个很好的例子:http://docs.strongloop.com/display/public/LB/Defining+boot+scripts#Definingbootscripts-Synchronousbootscripts - IvanZh
2个回答

10

您可以使用 ModelCtor.app.models.OtherModelName 来从自定义方法中访问其他模型。

/** common/models/product.js **/
module.exports = function(Product) {
  Product.createRandomName = function(cb) {
    var Randomizer = Product.app.models.Randomizer;
    Randomizer.createName(cb);
  }

  // this will not work as `Product.app` is not set yet
  var Randomizer = Product.app.models.Randomizer;
}

/** common/models/randomizer.js **/
module.exports = function(Randomizer) {
  Randomizer.createName = function(cb) {
    process.nextTick(function() { 
      cb(null, 'random name');
    });
  };
}

/** server/model-config.js **/
{
  "Product": {
    "dataSource": "db"
  },
  "Randomizer": {
    "dataSource": null
  }
}

如果您的代码中没有另一个模型构造函数,但是您有该模型的实例,该怎么办? - JBCP
1
@JBCP 你可以通过 constructor 属性(通常)获取构造函数。例如:"productInstance.constructor.app.models.Randomizer"。 - Miroslav Bajtoš
谢谢。如果我有一个模型实例,只做app.models.OtherModel是安全的吗?因为我知道应用程序现在已经初始化了。这是我迄今为止一直在做的。 - JBCP
1
@JBCP 很有可能(99%)是的。 - Miroslav Bajtoš

0

我知道这篇文章已经发布很久了。但是最近几天我得到了同样的问题,以下是我通过最新的Loopback API找出来的:

您可以按照以下方式获取您的模型所附加的应用程序:

ModelX.js

module.exports = function(ModelX) {
   //Example of disable the parent 'find' REST api, and creat a remote method called 'findA'
 var isStatic = true;
 ModelX.disableRemoteMethod('find', isStatic);

 ModelX.findA = function (filter, cb) {
      
      //Get the Application object which the model attached to, and we do what ever we want
 ModelX.getApp(function(err, app){
   if(err) throw err;
   //App object returned in the callback
   app.models.OtherModel.OtherMethod({}, function(){
   if(err) throw err;
   //Do whatever you what with the OtherModel.OtherMethod
      //This give you the ability to access OtherModel within ModelX.
      //...
      });
     });
 }
    
   //Expose the remote method with settings.
 ModelX.remoteMethod(
  'findA',
  {
  description: ["Remote method instaed of parent method from the PersistedModel",
   "Can help you to impliment your own business logic"],
  http:{path: '/finda', verb: 'get'},
    accepts: {arg:'filter', 
    type:'object', 
    description: 'Filter defining fields, where, include, order, offset, and limit',
  http:{source:'query'}},
   returns: {type:'array', root:true}
  }
 );
};

看起来我在这里的代码块格式上做得不太好...

另外,当调用'getApp'时,你应该注意时机,因为它很重要。如果你在初始化模型时过早地调用这个方法,可能会出现类似于'undefined'的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接