rishipr / mern-auth

MERN (+ Redux) user authentication boilerplate with Passport.js and JWT
616 stars 320 forks source link

How to mapStateToProps Id of the user to authAction #17

Closed BradPrit09 closed 4 years ago

BradPrit09 commented 4 years ago

@rishipr I need help . Please have a look . I mapped the mapStateToProps like this .

const mapStateToProps = state => ({
  auth: state.auth,
  user: state.user,
  errors: state.errors
});

from my component and when i do axios request in authAction , Do I need to pass directly user directly like

export const registerDevice = (userData, history) => dispatch => {
  axios
    .post("/api/users/addDevice/${user_id}", userData)
    .then(res => history.push("/Comment"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};

My back-end code is developed such that it takes farm id also,I can make a postman request it's working but I am unable to integrate with front-end part ,little.

//Router for Device Addition in a Specific Farm
//:id@ User Id
//:farmID@ Farm that we want to add the Device.  
router.post("/addDevice/:id/:farmID",(req,res) => {
  const deviceName=req.body.deviceName;
  const deviceType=req.body.deviceType;
  const description=req.body.description;
  const deviceLocation=req.body.deviceLocation;
  const Parameter=req.body.Parameter;
  const Topic=("sagita/"+User.name+"/"+req.params.farmID+"/"+deviceName+"/"+deviceLocation+"/"+Parameter);
  User.findByIdAndUpdate(req.params.id, 
    { $push: { "farm.$[farm].device": {deviceName,deviceType,description,deviceLocation,Parameter,Topic} },
     $currentDate:{lastModified:true,"deviceModified":{$type:"timestamp"}} },
      { arrayFilters: [ { "farm._id": req.params.farmID } ],upsert: true, safe: true, new: true, multi: false},
        function (err, updatedFarm)
         {
          if (err)
          console.log(err);
          res.json({success:true,message:"Farm updated! Roger_That.",body:updatedFarm});
          }); 
   User.aggregate([
    {$group:{ _id:"$device",data:{$push:"$Topic"}} },

    {$out:"Data Bucket",}
  ]);

});

Originally posted by @BradPrit09 in https://github.com/rishipr/mern-auth/issues/15#issuecomment-559104344

rishipr commented 4 years ago

export const registerDevice = (userData, history) => dispatch => { axios .post("/api/users/addDevice/${user_id}", userData) .then(res => history.push("/Comment")) .catch(err => dispatch({ type: GET_ERRORS, payload: err.response.data }) ); };

The user_id variable seems undefined. Where is it coming from? Is it part of the userData object? If so, would have to say userData.user_id.

Would add a console.log in your action to make sure it is being fired off, and see what it included in the userData parameter. Hope that is helpful.

rishipr commented 4 years ago

Closing this issue as there seem to be no follow-ups. Hope you were able to figure it out!