【问题标题】:{message: “Make sure to call fetch to execute the query” but i’m using paginate in adonis / mode.js{消息:“确保调用 fetch 来执行查询”但我在 adonis / mode.js 中使用分页
【发布时间】:2020-03-14 05:43:49
【问题描述】:

在查询生成器的文档中,说在查询后我必须创建一个.fetch().paginate() 但在这种情况下我收到:

{message: "确保调用 fetch 来执行查询"

我尝试了什么:

 async show({request}){

     const { page, pageSize } = request.get();
     const filter = request.input('filter')

     const bIds = await BookUnit
                         .query()
                         .where('book_id', request.params.id)
                         .ids() 

     const questions = await Question
                             .query()
                             .whereIn('book_unit_id', bIds)
                             .with('book_unit')


     //Filtros
     if(filter){
         if(filter.search("description") !== -1){
             let description = filter.match(/(?<=description~contains~').*?(?=')/)
             questions.where('description', 'ilike', '%'+description[0]+'%')
         }
         if(filter.search('unit') !== -1){
             let unit = filter.match(/(?<=unit~contains~').*?(?=')/)
             questions.where('unit', '=', unit[0])
         }
     }

     return await questions.paginate(page,pageSize)

我的模型:

class Book extends Model {

    book_units() {
        return this.hasMany("App/Models/BookUnit").select(
          "id",
          "description as unit"
        );
      }

      book_unit_questions() {
        return this.manyThrough("App/Models/BookUnit", "book_unit_questions");
      }

      user () {
        return this.belongsTo('App/Models/User')
            .select('id', 'username')
    }

}

class BookUnit extends Model {

    static get table () {
        return 'book_unit'
    }

    book_unit_questions() {
        return this.hasMany('App/Models/BookUnitQuestion')
    }


    user () {
        return this.belongsTo('App/Models/User')
            .select('id', 'username')
    }

}

class BookUnitQuestion extends Model {

    static get table () {
        return 'book_unit_question'
    }

    user () {
        return this.belongsTo('App/Models/User')
            .select('id', 'username')
    }

    book_unit(){
        return this.belongsTo('App/Models/BookUnit')
            .select('id','description', 'sequence', 'unit')
    }

    book(){
        return this.belongsTo('App/Models/Book')
    }

    book_unit_questions() {
        return this.belongsTo('App/Models/BookUnit')
    }

}

抱歉,我无法发布我的迁移,因为我收到消息“看起来您的帖子主要是代码;请添加更多详细信息”,如果您可以创建聊天,我可以发送

@编辑:

我试过了:

    async show({ request }){

        const { page, pageSize } = request.get();
        const filter = request.input('filter')

        const bIds = await BookUnit
               .query()
               .where('book_id', request.params.id)
               .ids()

                if (filter) {
                    if (filter.search("description") !== -1) {
                        let description = filter.match(/(?<=description~contains~').*?(?=')/)
                        questions.where('description', 'ilike', '%' + description[0] + '%') //Add new condition
                    }
                    if (filter.search('unit') !== -1) {
                        let unit = filter.match(/(?<=unit~contains~').*?(?=')/)
                        bIds.where('unit', '=', unit[0]) //Add new condition
                    }
                }

        const questions = Question
             .query()
             .whereIn('book_unit_id', bIds)
             .with('book_unit')



        if (filter) {
            if (filter.search("description") !== -1) {
                let description = filter.match(/(?<=description~contains~').*?(?=')/)
                questions.where('description', 'ilike', '%' + description[0] + '%') //Add new condition
            }
        }

        return await questions.paginate(page, pageSize) //Now query is executed

but now i have:

> bIds.where is not a function",

@Edit new tentative:

async show({ request }){

        const { page, pageSize } = request.get();
        const filter = request.input('filter')

        const bIds = await BookUnit
               .query()
               .where('book_id', request.params.id)
               .ids()

        const questions = Question
             .query()
             .with('book_unit', (builder) => {
                if(filter){
                    if (filter.search('unit') !== -1) {
                        let unit = filter.match(/(?<=unit~contains~').*?(?=')/)
                        builder.where('unit', '=', unit[0]) //Add new condition
                    }
                }
            })
            .whereIn('book_unit_id', bIds)

        //Filtros
        if (filter) {
            if (filter.search("description") !== -1) {
                let description = filter.match(/(?<=description~contains~').*?(?=')/)
                questions.where('description', 'ilike', '%' + description[0] + '%') //Add new condition
            }
        }

        return await questions.paginate(page, pageSize) //Now query is executed
} [![enter image description here][1]][1]

找不到单位,但问题继续显示在我的网格中...

【问题讨论】:

  • 你能分享你的迁移文件+模型吗?
  • @CrBast 请检查我的编辑

标签: node.js adonis.js


【解决方案1】:

您必须fetchpaginate 查询结果。

我的解决方案是只在最后执行查询并一一添加条件。喜欢:

const page = 1
const pageSize = 2
const filter = request.input('filter')

const bIds = await BookUnit
       .query()
       .where('book_id', 2)
       .ids()

/*
Remove await -> Query is not executed yet
See below
*/
const questions = Question
     .query()
     .whereIn('book_unit_id', bIds)
     .with('book_unit')

//Filtros
if (filter) {
    if (filter.search("description") !== -1) {
        let description = filter.match(/(?<=description~contains~').*?(?=')/)
        questions.where('description', 'ilike', '%' + description[0] + '%') //Add new condition
    }
    if (filter.search('unit') !== -1) {
        let unit = filter.match(/(?<=unit~contains~').*?(?=')/)
        questions.where('unit', '=', unit[0]) //Add new condition
    }
}

return await questions.paginate(page, pageSize) //Now query is executed


信息

在我的测试中,我没有使用带有过滤器的部件。我使用了一个简单的条件。喜欢:

questions.where('book_unit_question.description', 'like', '%1%') // Test add new condition

文档:How to prevent Knex.js from running a query object when returning it from an async function?

【讨论】:

  • 问题是单位不是问题的属性,而是 bookUnit 的属性...
  • @CrBast 你能看到我的编辑吗?我尝试用其他方式,但我不断有错误
  • 实际上我正在使用运行时约束,但正在过滤 book_units 数组,所以,如果我没有找到一个单元,我的问题会继续显示在我的网格中。请看我的编辑。
  • bIds.where('unit', '=', unit[0]) //Add new condition 错误 -> 因为 bIds = array []
  • @CrBast 我没有使用 where 出价。我尝试: .with('book_unit', (builder) => { if(filter){ if (filter.search('unit') !== -1) { let unit = filter.match(/(?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多