Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

React and Node js:Bug Fixing

How to get particular object of collection using foreign objectid
How to get json object multiple values or properties from frontend
get single question and set a variable for task created by user
how to format a date and time
How to send some parameter to appcontext by excluding using function

How to sets a minimum date allowed for the input field in datepicker

Task1

Image description

In backend

Image description

How to get particular object of collection using foreign objectid

const getSingleTask = async (req, res) => {
  const { id: taskId } = req.params
  if (!taskId) {
    throw new BadRequestError("please provide task id")
  }
  const task = await Task.findOne({ _id: taskId })
  const task_createdby = task.createdBy
  console.log(task_createdby)
  const user = await User.findById(task_createdby)
  console.log(user)
  if (!task) {
    throw new NotFoundError("no task with this id")
  }   
  if(task.createdBy)
  {
    const task_createdby = task.createdBy 
    const user = await User.findById(task_createdby)      
    res.status(200).json({ task,createdby:user.name })
  }
  else
  {      
    res.status(200).json({ task })
  }  
}
Enter fullscreen mode Exit fullscreen mode

How to get json object multiple values or properties from frontend

In app.context

  const getSingleTask = async (id) => {
    try {
      const response = await authFetch.get(`task/${id}`)
      return response.data
    } catch (error) {}
  }
Enter fullscreen mode Exit fullscreen mode

use setter method

const [taskcreatedby, setTaskcreatedby] = useState('');
  const [taskcreatedby_time, setTaskcreatedbytime] = useState('');
  const [taskupdatedbytime, setTaskupdatedbytime] = useState('');
Enter fullscreen mode Exit fullscreen mode

get single question and set a variable for task created by user

how to format a date and time

  useEffect(() => {

    if (editingTask !== null) {
      getSingleTask(editingTask._id).then((data) => {        
         const createdBy = data.createdby; 
         const time = data.task.createdAt; 
         const updatedtime = data.task.updatedAt; 
         const formattedTime = moment(time).format('MMMM Do YYYY, h:mm:ss a');
         const formattedupdateTime = moment(updatedtime).format('MMMM Do YYYY, h:mm:ss a');
         const createdbytime = createdBy + " In " + formattedTime;
         console.log("task createdBy")    
         setTaskcreatedby(createdBy)
         setTaskcreatedbytime(createdbytime)
         setTaskupdatedbytime(formattedupdateTime)

        setTask(data.task)
        if (data.task.goal) {
          const goalTitle = goals.find((i) => i._id === data.task.goal)
          // console.log(goalTitle)
          if (goalTitle) {
            setValues({ ...values, goalTitle: goalTitle.title })
          }
        }
      })
    }
Enter fullscreen mode Exit fullscreen mode

Image description
Display the task created by and time

 {taskcreatedby?(<label>Created by:{taskcreatedby_time} </label>):null}
     <label>Task Updated:{taskupdatedbytime} </label>
Enter fullscreen mode Exit fullscreen mode

Task2

After editing name should not updated

Image description

Image description

 const { user } = useAppContext()
Enter fullscreen mode Exit fullscreen mode
useEffect(() => {
    setValues({
      ...values,
      name: user.name,
      userId: user._id,
      userRole: user.role,
      taskId: editingTask._id,
      projectId: selectedProject,
      profilePic: user.profileUrl,
    })
  }, [isWorklogModalOpen])
Enter fullscreen mode Exit fullscreen mode
const handleSubmit = (e) => {
    e.preventDefault()

   if (isEditingWorklog) {
      if (values.workDescription !== "") {
        const regex = /^(\d+)(m|h|d|w)(\s(\d+)(m|h|d|w))*$/g
        if (!regex.test(values.timeSpent)) {
          toast.error("enter value in m h d w")
        }
        else
        {
        console.log("Values before dispatch:", values);
        const { name, ...filteredValues } = values;
        console.log("Values after dispatch:", filteredValues);
        dispatch(
          updateWorklog({
            ...filteredValues,
            _id: editingWorklog._id,
            timeSpent: getMinutes(values.timeSpent)
          })
        )
        setValues(initialState)
        dispatch(closeWorklogModal())
      }
Enter fullscreen mode Exit fullscreen mode

How to send some parameter to appcontext by excluding using function

  console.log("Values before dispatch:", values);
        const { name, ...filteredValues } = values;
        console.log("Values after dispatch:", filteredValues);

  dispatch(
          updateWorklog({
            ...filteredValues,
            _id: editingWorklog._id,
            timeSpent: getMinutes(values.timeSpent)
          })
Enter fullscreen mode Exit fullscreen mode

How to sets a minimum date allowed for the input field in datepicker

Image description

Image description

==============================

Image description

**
solution**
index.js

export { raiseTicket, getAllTicket, closeTicket,getAlluserTicket } from "./ticket/ticketActions"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)