So I'm trying to debug why my array of questions is not being properly populated in my mobx-state-tree questions-store. Nothing after the yield statement in the getQuestions action runs.
export const QuestionStoreModel = types .model("QuestionStore") .props({ questions: types.optional(types.array(QuestionModel), []) }) .extend(withEnvironment) .views((self) => ({})) // eslint-disable-line @typescript-eslint/no-unused-vars .actions((self) => ({ saveQuestions: (questionSnapshots: QuestionSnapshot[]) => { console.tron.log("SAVE QUESTIONS"); console.tron.log({ questionSnapshots }); const questionModels: Question[] = questionSnapshots.map(c => QuestionModel.create(c)); self.questions.replace(questionModels); // Replace the existing data with the new data } })) .actions((self) => ({ getQuestions: flow(function*() { console.tron.log("GET QUESTIONS"); const result: GetQuestionsResult = yield self.environment.api.getQuestions(); // Nothing after this yield statement runs console.tron.log("AFTER GET QUESTIONS"); if (result.kind === "ok") { self.saveQuestions(result.questions); } else { __DEV__ && console.tron.log(result.kind); } }) }))
This is the api's getQuestions
async getQuestions(): Promise<Types.GetQuestionsResult> { // make the api call const response: ApiResponse<any> = await this.apisauce.get("", { amount: API_PAGE_SIZE }) console.tron.log({response}); // the typical ways to die when calling an api if (!response.ok) { const problem = getGeneralApiProblem(response); if (problem) return problem; } console.tron.log('AFTER OK CHECK') // transform the data into the format we are expecting try { const rawQuestion = response.data.results; console.tron.log({rawQuestion}); const convertedQuestions: QuestionSnapshot[] = rawQuestion.map(convertQuestion); console.tron.log({convertedQuestions}); return { kind: "ok", questions: convertedQuestions }; } catch (e) { __DEV__ && console.tron.log(e.message); return { kind: 'bad-data' } } }
And the Question Model:
export const QuestionModel = types .model("Question") .props({ id: types.identifier, category: types.maybe(types.string), type: types.enumeration(['multiple', 'boolean']), difficulty: types.enumeration(['easy', 'medium', 'hard']), question: types.maybe(types.string), correctAnswer: types.maybe(types.string), incorrectAnswers: types.optional(types.array(types.string), []), guess: types.maybe(types.string) }) .views((self) => ({ get allAnswers() { return shuffle(self.incorrectAnswers.concat([self.correctAnswer])) }, get isCorrect() { return self.guess === self.correctAnswer; } })) .actions((self) => ({ setGuess(guess: string) { self.guess = guess; } })) type QuestionType = Instance<typeof QuestionModel>export interface Question extends QuestionType {}type QuestionSnapshotType = SnapshotOut<typeof QuestionModel>export interface QuestionSnapshot extends QuestionSnapshotType {}export const createQuestionDefaultModel = () => types.optional(QuestionModel, {})
As you can see from the logs, the api call is successful and convertedQuestions logs properly but nothing is recorded after that. Maybe I'm misunderstanding how generator functions work but shouldn't it resume after the yielding function returns a value?
Any insights would be greatly appreciated.