In the backend I can use my Token to search after Notes which User creates.
In my Frontend project it doesn´t show me my Posts ..
I store my token in localStorage “userInfo” .. so it can be taken from there if I login to my account.
my NotesSchema :
const forumSchema = ({
forumName: {
type: String,
required: true,
},
forumDescription: {
type: String,
required: true,
},
createdBy: {
type: Schema.Types.ObjectId, ref: 'User'
},
published_on: {
type: String,
default: moment().format("LLL")
},
});
My note route :
router.get('/getByOwnerID',verifyToken,ForumService.getByToken);
And the function which I wrote for that :
exports.getByToken = async (req, res, next) => {
Forum.find({ userID: req.token.userID })
.then(doc => {
if(!doc) { return res.status(400).end();}
return res.status(200).json(doc);
})
.catch(err => next(err));
}
And the frontend noteAction.js:
export const listNotes = () => async (dispatch, getState) => {
try{
dispatch({
type: NOTES_LIST_REQUEST,
});
const {
userLogin: { userInfo },
} = getState();
const config = {
headers: {
Authorization: `Bearer ${userInfo.token}`,
},
};
const { data } = await axios.get(`http://localhost:8080/forum/getByOwnerID`, config);
dispatch({
type: NOTES_LIST_SUCCESS,
payload: data,
});
}catch(error){
const message =
error.response && error.response.data.message
? error.response.data.message
: error.message;
dispatch({
type: NOTES_LIST_FAIL,
payload: message,
});
}
}
I hope it´s clear if not I can add Information ..