根据用户类型 MERN 显示问题

display questions based on user type MERN

我有两个集合,一个是针对管理员的问题,另一个是针对用户的问题。 我无法使用 redux store 在 React 中显示问题。

store/action

import { QUESTIONS } from '../../constants/actionTypes';
import * as api from '../../services/api';
import * as paths from '../../constants/apiPaths';


const TOTAL_QUESTIONS = 60;


export const fetcQuestions = (router) => async (dispatch) => {
  try {
    const user = await api.get(paths.FETCH_USER);
    const userType = user.type;
    console.log(userType + "userType");
    if((userType === "Student") || (userType === "STUDENT"))
    {
    const {questions, assessment, options} = await api.get(paths.FETCH_QUESTIONS);
    console.log(api.get(paths.FETCH_QUESTIONS) + "Question path");
    dispatch({ type: QUESTIONS, questions, assessment, options });
    if(assessment.responded === TOTAL_QUESTIONS) {
      router.push('/advice');
    }
    }
    else
    if((userType === "Admin") || (userType === "ADMIN"))
    {
      console.log(userType + "type of user");
      const {questions, assessment, options} = await api.get(paths.FETCH_QUESTIONS);
    console.log(api.get(paths.FETCH_QUESTIONS) + "Question path");
    dispatch({ type: QUESTIONS, questions, assessment, options });
    if(assessment.responded === TOTAL_QUESTIONS) {
      console.log("thank you");
    }
    }
   
  } catch (error) {          
    console.log(error);
  }
};

export const postAssessment = (data, router) => async (dispatch) => {
  try {
    const {questions, assessment, options} = await api.post(paths.POST_ASSESSMENT, data);
    console.log(paths.POST_ASSESSMENT + "assessment");
    dispatch({ type: QUESTIONS, questions, assessment, options });
    if(assessment.responded === TOTAL_QUESTIONS) {
    console.log("Thank you");
    }
  } catch (error) {
    console.log(error);
  }
};

请帮我看看我在这里做错了什么。谢谢。

减速器:

import * as actionType from '../../constants/actionTypes';

const assessmentReducer = (state = { questions: null, assessment: null, options: null }, action) => {
  switch (action.type) {
    case actionType.QUESTIONS:
      return { ...state, questions: action?.questions, assessment: action?.assessment, options: action?.options, loading: false, errors: null };
    default:
      return state;
  }
};

export default assessmentReducer;

NodeJS 控制器:

const TOTAL_QUESTIONS = 120;


export const fetchQuestions = async (req, res) => {
  try {
    const user = await db.findOne('USER', { _id: req.userId});
    console.log(user + "user data");
    let answerdQuestions = [];
    let nextQuestions;
    let assessment;
    if (user.assessment) {
      assessment = await db.findOne('ASSESSMENT', { user: req.userId });
      answerdQuestions = assessment.responses.map(response => response.number)
    }
    nextQuestions = getNextQuestions(answerdQuestions);
    if ((user.type === "STUDENT") || (user.type === "student")) {
      console.log(user.type + "type");
    const questions = await db.find('QUESTIONG', { number: { $in: nextQuestions } });
    console.log(questions.number + "quesstudent");
    res.status(200).json({ questions, assessment: { id: assessment?._id, responded: answerdQuestions.length }, options: options });
    return answerdQuestions;
    }
    else {
      console.log(user.type + "typegh");
    const questions = await db.find('QUESTION', {number: { $in: nextQuestions }});
    console.log(questions.question + "quesdata");
    res.status(200).json({ questions, assessment: { id: assessment?._id, responded: answerdQuestions.length }, options: options });
    return answerdQuestions;
    }
  
  } catch (err) {
    console.log(err)
    res.status(500).json({ message: "Something went wrong" });
  }
};

export const postResponses = async (req, res) => {
  try {
    let responses = req.body.responses;
    let assessmentId = req.body.id;
    responses = responses.map(response => {
      return {
        score: Number(response.value),
        number: response.number,
        category: response.category,
        question: response._id
      }
    });

    const user = await db.findOne('USER', {_id: req.userId});
      console.log( user.type + "typeofuser");
    let assessment = await db.findOne('ASSESSMENT', { _id: assessmentId });
    if (assessment?.responses.length === TOTAL_QUESTIONS) {
      res.status(200).json("completed");
    }
    if (!assessment) {
      // 
      let response = {
        user: req.userId,
        responses: responses,
        responded: responses.length
      }
      assessment = await db.create('ASSESSMENT', response);
      await db.findOneAndUpdate('USER', { _id: req.userId }, { assessment: assessment._id });
      
  
    } else {
      assessment = await db.findOneAndUpdate('ASSESSMENT', { _id: assessment._id }, { $push: { responses: { $each: responses } } });
    }
    let answerdQuestions = assessment.responses.map(response => response.number);
    const nextQuestions = getNextQuestions(answerdQuestions);
    if (answerdQuestions.length === TOTAL_QUESTIONS) {
      console.log("You win");
      ]
     
   }
  if((user.type === "STUDENT") || (user.type==="student") ){
    console.log("type" + user.type);
    const questions = await db.find('QUESTIONG', { number: { $in: nextQuestions } });
    res.status(200).json({ questions, assessment: { id: assessment?._id, responded: answerdQuestions.length }, options: options });
  } else
  {
    console.log(user.type + "typeg");
    const questions = await db.find('QUESTION', { number: { $in: nextQuestions } });
    res.status(200).json({ questions, assessment: { id: assessment?._id, responded: answerdQuestions.length }, options: options });
 
  }
  
}catch (err) {
    console.log(err)
    res.status(500).json({ message: "Something went wrong" });
  }
};

//export default fetchQuestions;

我还添加了 reducer 和 NodeJS 控制器,根据用户类型显示问题。请帮忙。如果类型是学生,则不会显示问题,否则会显示问题

我看到的是,当您尝试获取用户类型 student 的数据时,您提到了错误的数据库。这是您的代码:

if (user.type === 'STUDENT' || user.type === 'student') {
  console.log(user.type + 'type');
  const questions = await db.find('QUESTIONG', { number: { $in: nextQuestions } });

您需要更正数据库名称,一切都会正常:

const questions = await db.find('QUESTION', { number: { $in: nextQuestions } });

希望这有用!!!