Sails.js /水线:执行水线查询模型中的功能的toJSON? [英] Sails.js/waterline: Executing waterline queries in toJSON function of a model?

查看:126
本文介绍了Sails.js /水线:执行水线查询模型中的功能的toJSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的模型,假设它描述了一本书:

  module.exports = {    属性:{
        名称: {
            输入:'串',
            要求:真
        },
        类型:{
            类型:'串',
            要求:真
        },
        book_id:{
            类型:'整'
        }
    }
}

下面的类型属性是书籍的类型,例如HorrorBook。我有HorrorBooks,FantasyBooks和这样一个单独的模型,因为模型的结构彼此不那么多,他们有其他字段没有等,这就是为什么我不能只用一个通用的书本 - 模型。 HorrorBook,FantasyBook等每个人都有自己的身份证字段,这意味着既可以是HorrorBook并使用相同ID的FantasyBook。当创建的书,我有一个beforeCreate函数,它还会在HorrorBook或FantasyBook取决于书的型属性的条目。

我想要做的是,在查询书上从前端,我想包括书在信息属性的相关信息,这是例如HorrorBook条目。

 的toJSON:功能(){
    变种物镜= this.toObject();
    开关(obj.type){
        案HorrorBook:
            HorrorBook.find({ID:obj.book_id})
                .exec(函数(ERR,book_info){
                    obj.book_info = book_info;
                    返回OBJ;
                })
    }
}

然而,这导致的toJSON刚刚返回null一路。什么我假设是,查询是异步的,使得它太慢或者什么的,所以它不会使它的回报的呼叫都没有。所以,我该怎么处理这件事?有没有办法在我想要成功,还是应该以某种方式改变我的做法?


解决方案

  

什么我假设是,查询是异步的,使得它太慢或者什么的,所以它不会使它的'回报'叫的。


这是怎么回事的是,的toJSON()是sync'ed和它的返回什么出来 HorrorBook.find({ID的:obj.book_id })。EXEC()未定义听起来是正确的。

如果你需要异步您最好的选择运行的东西是创建一个异步的toJSON()实例方法,例如(未测试):

  toJSONAsync:函数(回调){
  变种物镜= this.toObject();
  开关(obj.type){
    案HorrorBook:
        HorrorBook.find({ID:obj.book_id})
            .exec(函数(ERR,book_info){
                obj.book_info = book_info;
                回调(OBJ);
            })
  }
}

现在,你可以通过做让你的结果是:

  book.toJSONAsync(函数(JSON){
  的console.log(JSON);
})

I have the following model, let's say it describes a book:

module.exports = {

    attributes: {
        name: {
            type: 'String',
            required: true
        },
        type: {
            type: 'string',
            required: true
        },
        book_id: {
            type: 'integer'
        }
    }
}

Here the 'type' attribute is the genre of the book, for example HorrorBook. I have a separate model for HorrorBooks, FantasyBooks and such, since the structure of the models differs from each other so much, they have fields the other doesn't have etc, which is why I can't just use a single generic Book-model. HorrorBook, FantasyBook etc each have their own 'id' fields, meaning there can be both a HorrorBook and a FantasyBook with the same id. When creating the book, I have a beforeCreate function that also creates an entry in HorrorBook or FantasyBook depending on the 'type' attribute of the book.

What I'm trying to do is that on queries on Book from the frontend, I'd like to include the relevant information of the book in an 'information' attribute, which is for example a HorrorBook entry.

toJSON: function() {
    var obj = this.toObject();
    switch (obj.type) {
        case 'HorrorBook':
            HorrorBook.find({id: obj.book_id})
                .exec(function(err, book_info) {
                    obj.book_info = book_info;
                    return obj;
                })
    }
}

However, this leads to the toJSON just returning null all the way. What I'm assuming is that the query being asynchronous makes it too slow or something, so it doesn't make it to the 'return' call at all. So what should I do about this? Is there a way to succeed in what I'm trying, or should I change my approach somehow?

解决方案

What I'm assuming is that the query being asynchronous makes it too slow or something, so it doesn't make it to the 'return' call at all.

What's happening is that toJSON() is sync'ed and it's returning whatever comes out of HorrorBook.find({id: obj.book_id}).exec(), undefined sounds about right.

If you need to run something async your best bet is to create an async toJSON() instance method, for example (not tested):

toJSONAsync: function(callback) {
  var obj = this.toObject();
  switch (obj.type) {
    case 'HorrorBook':
        HorrorBook.find({id: obj.book_id})
            .exec(function(err, book_info) {
                obj.book_info = book_info;
                callback (obj);
            })
  }
}

And now you can get your result by doing:

book.toJSONAsync(function(json){
  console.log(json);
})

这篇关于Sails.js /水线:执行水线查询模型中的功能的toJSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆