1
import Question from "../models/Question.ts";
import IQuestionnaireQuestion from "../interfaces/IQuestionnaireQuestion";
import baseQuestions from "../db/basequestions.ts";

const addQuestion = async (question: IQuestionnaireQuestion): Promise<any> => {
  try {
    const newQuestion = new Question(question);
    const questionExists =
      (await Question.exists({ text: question.text })) ||
      (await Question.exists({ quizText: question.quizText }));
    if (!questionExists) {
      await newQuestion.save();
      console.log(newQuestion);
      return newQuestion;
    }
    return null;
  } catch (e) {
    console.error(`Issue adding question: ${e}`);
    return null;
  }
};

export default {
  getQuestions: async (): Promise<any> => {
    try {
      const questions = await Question.find();
      return questions;
    } catch (e) {
      console.error(`Issue getting questions: ${e}`);
      return [];
    }
  },
  addQuestion,
  getRandomQuestions: async (numQuestions: number): Promise<any> => {
    try {
      const questions = await Question.aggregate([
        { $sample: { size: numQuestions } },
      ]);
      return questions;
    } catch (e) {
      console.error(`Issue getting random questions: ${e}`);
      return [];
    }
  },
  addBaseQuestions: async (): Promise<any> => {
    for (const thisQuestion of baseQuestions) {
      const formattedQuestion: IQuestionnaireQuestion = {
        text: thisQuestion.text,
        quizText: thisQuestion.quizText,
        fakeAnswers: thisQuestion.fakeAnswers,
      };
      await addQuestion(formattedQuestion);
    }
  },
};
Module parse failed: Unexpected token (5:35)
You may need an appropriate loader to handle this file type.
| import baseQuestions from "../db/basequestions.ts";
| 
> const addQuestion = async (question: IQuestionnaireQuestion): Promise<any> => {
|   try {
|     const newQuestion = new Question(question);

This code randomly stopped working. I have tried reinstalling node, babel, and typescript. Help would be greatly appreciated! Project using react, typescript, and socketio. This code randomly stopped working. I have tried reinstalling node, babel, and typescript. Help would be greatly appreciated! Project using react, typescript, and socketio.

tyler
  • 11
  • 1

0 Answers0