Thread ID: |
Created: |
Updated: |
Platform: |
Replies: |
149221 | Nov 19,2019 11:10 AM UTC | Nov 25,2019 09:30 AM UTC | Angular - EJ 2 | 7 |
![]() |
Tags: Schedule |
app.post("/BatchData", (req, res) => {
console.log(req.body);
var eventData = [];
if (req.body.action == "insert" || (req.body.action == "batch" && req.body.added.length > 0)) {
(req.body.action == "insert") ? eventData.push(req.body.value) : eventData = req.body.added;
for (var i = 0; i < eventData.length; i++) {
var sdate = new Date(eventData[i].StartTime);
var edate = new Date(eventData[i].EndTime);
eventData[i].StartTime = (new Date(+sdate - (sdate.getTimezoneOffset() * 60000)));
eventData[i].EndTime = (new Date(+edate - (edate.getTimezoneOffset() * 60000)));
eventData[i].CreatedBy = //here i want to add the users emailadress from the token;
dbo.collection('ScheduleData').insertOne(eventData[i]);
}
}
I tried to add the token middleware to the function, like this:
app.post("/BatchData", authRequired, (req, res) => {
but as soon i add that extra argument, it stops retrieving the appointments, without any error message. This is my authRequired function:
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const config = require('../config/database')
function authRequired(req, res, next) {
const token = req.get("authorization");
if (!token) {
res.json({ success: false, message: "No token provided" });
} else {
jwt.verify(token, config.secret, (err, decoded) => {
if (err) {
res.json({ success: false, message: "Token is invalid" + err });
} else {
req.decoded = decoded;
next();
}
});
}
}
module.exports = {
authRequired: authRequired
};
Another option would be to send the emailaddress via the frontend. How do I add a custom field 'createdBy' (without any graphical input elements) to send a value from my component along with the appointment data in the Mongo collection? Do I add that in the onPopUpOpen() function?
Can you maybe help me on how to put users emailadres there at the 'createdBy' field? I retrieve the users info by this code:
ngOnInit() {
this.auth.getProfile().subscribe((profile : any) => {
this.userName = profile.user.username;
this.email = profile.user.email;
console.log(profile.user.email)
})
}If you need any more info, please let me know
Thanks in advance
onPopupOpen(args: PopupOpenEventArgs): void {
if (args.type === 'Editor') {
// Create required custom elements in initial time
if (!args.element.querySelector('.custom-field-row1')) {
let row: HTMLElement = createElement('div', { className: 'custom-field-row1' });
let formElement: HTMLElement = <HTMLElement>args.element.querySelector('.e-schedule-form');
formElement.firstChild.insertBefore(row, args.element.querySelector('.e-title-location-row'));
let container: HTMLElement = createElement('div', { className: 'custom-field-container1' });
let inputEle: HTMLInputElement = createElement('input', {
className: 'e-field', attrs: { name: 'CreatedBy' }
}) as HTMLInputElement;
container.appendChild(inputEle);
row.appendChild(container);
let inputobj: TextBox = new TextBox({
placeholder: 'CreatedBy',
});
inputobj.appendTo(inputEle);
inputEle.setAttribute('name', 'CreatedBy');
}
}
} |
export class ScheduleComponent implements OnInit {
userName;
email;
ngOnInit() {
this.auth.getProfile().subscribe((profile : any) => {
this.userName = profile.user.username;
this.email = profile.user.email;
console.log(profile.user.email)
})
}I hope i made my point clearer now :). I want to put that email variable into 'createdBy' but that should not be entered in the onPopUpOpen event
onActionBegin(args: ActionEventArgs): void {
if (args.requestType === "eventCreate") {
args.data[0].CreateBy = this.email;
args.data[0].UserName = this.userName;
}
} |
app.post("/GetData", (req, res) => {
debugger;
dbo.collection('ScheduleData').find({'CreatedBy':'testUser'}).toArray((err, cus) => {
res.send(cus);
});
});
But not static 'testUser' but the username that is stored in the a token in localstorage called 'User' .
I have another method for getting the logged in users profile like this:
router.get('/profile', authRequired, (req, res) => {
User.findOne({_id: req.decoded.userId}).select('username email').exec((err, user) =>{
if(err){
res.json({success: false, message: err});
} else {
if(!user){
res.json({success: false, message: 'Not found'});
} else {
res.json({success: true, user: user})
}
}
});
});However, if I add the middleware 'authRequired' into the '/getData' endpoint, it stops working.How can I retrieve the logged in users email in order to assign that to the find({}) query?
ngOnInit() {
this.userName = JSON.parse(localStorage.getItem("user"));
console.log(this.userName.username)
this.userName = this.userName.username
}
So how can I query so that 'createdBy' equals this.userName ?
I hope I made it clearer. Thanks in advance.
App.component.ts file:
ngOnInit(): void {
this.selectedDate = new Date(2018, 1, 14);
this.email = "username@gmail.com"; // Here we used static email address for your reference. Kindly map your currently logined user email to achieve your scenario.
this.userName = "usename"; // Here we used static user name for your reference. Kindly map your currently logined username to achieve your scenario.
this.data = { Name: this.userName, Email: this.email };
this.dataQuery = new Query().addParams('tokens', this.data as any);
this.eventSettings = { dataSource: this.dataManager, query: this.dataQuery };
}
Server.js file:
app.post("/GetData", (req, res) => {
var query = { CreateBy: req.body.params.tokens.Email };
dbo.collection('ScheduleData').find(query).toArray((err, cus) => {
res.send(cus);
});
}); |
This post will be permanently deleted. Are you sure you want to continue?
Sorry, An error occured while processing your request. Please try again later.
This page will automatically be redirected to the sign-in page in 10 seconds.