简体   繁体   English

如何正确解决此knex.js承诺?

[英]How do I resolve this knex.js promise correctly?

I'm using knex and this is my code to get a question_info object after requesting from the database. 我正在使用knex,这是我从数据库请求后获取question_info对象的代码。

let question_info = knex.select('id', 'question', 'pre_question', 'response_options_id').from('_questions').whereNot({'progress': 100}).andWhere({'school_id': school_id}).orderBy('date_created', 'asc')
        .map((question) => {
            //going through all possible questions
            helpers.debug("Question", question);
            //checking if there are responses
            return knex('_responses').where({'question_id': question.id, 'student_id': event.user.id}).first()
                .then((response) => {
                    //if no responses, return question_info object
                    if(!response){
                        return knex.select('options_json').from('_response_options').where({'id': question.response_options_id}).first()
                            .then((options) => {
                                return {
                                    question_id: question.id,
                                    question: question.question,
                                    pre_question: question.pre_question,
                                    response_options: JSON.parse(options.options_json)
                                }
                            });
                    }
                });     
        }).catch((e) => {
            console.error('Error', e);
        });

When the first question has already been answered, my return value shows up as null. 当第一个问题已经回答时,我的返回值显示为null。 How do I just have the returned array without null using knex correctly. 我如何正确地使用knex使返回的数组不包含null。

[
  null,
  {
    "question_id": "2",
    "question": "What is second question?",
    "pre_question": "Hey!!",
    "response_options": [
      {
        "title": "Yes",
        "value": "Yes"
      },
      {
        "title": "Maybe",
        "value": "Maybe"
      },
      {
        "title": "No",
        "value": "No"
      }
    ]
  },
  {
    "question_id": "3",
    "question": "Third Question?",
    "pre_question": "Yo",
    "response_options": [
      {
        "title": "Yes",
        "value": "Yes"
      },
      {
        "title": "Maybe",
        "value": "Maybe"
      },
      {
        "title": "No",
        "value": "No"
      }
    ]
  }
]

Looks like your problem is that if second query indeed did return response you are not returning anything. 看起来您的问题是,如果第二个查询确实返回了response那么您将不会返回任何内容。

// this part of your code seems to be wrong
if (!response) {
  // return choices because there was no response found
  return knex('choices')...
}

It should be something like this: 应该是这样的:

// this part of your code seems to be wrong
if (!response) {
  // return choices because there was no response found
  return knex('choices')...
} else {
  return response;
}

EDIT this would be more or less how to do the same stuff with single query and joins (I didn't test this so it probably doesn't work, but it should give general idea how to achieve it): 编辑这将或多或少地是如何通过单个查询和联接来完成相同的工作(我没有对此进行测试,因此它可能不起作用,但是应该给出实现该方法的一般思路):

let question_info = knex('_questions as q')
  .select(
     'id', 
     'question', 
     'pre_question', 
     'response_options_id',
     'r.id as r_id',
     'options_json'
  )
  .join('_responses as r', builder => {
    builder
      .on('q.id', 'r.question_id')
      .onVal('r.student_id', event.user.id);
  })
  .join('_response_options as ro', 'q.response_options_id', 'ro.id')
  .where('q.school_id', school_id)
  .whereNot('q.progress', 100)
  .whereNull('r.id')
  .orderBy('q.date_created', 'asc')
  .map(notAnsweredQuestion => {
    return {
      question_id: notAnsweredQuestion.id,
      question: notAnsweredQuestion.question,
      pre_question: notAnsweredQuestion.pre_question,
      response_options: JSON.parse(notAnsweredQuestion.options_json)
    };
  }).catch((e) => {
    console.error('Error', e);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM