I am currently working on a project using Angular and I need to conditionally display a form based on certain values. I have successfully tested the backend route using Postman and everything is functioning correctly. Here is a snippet of my code:
Blockquote Angular Component HTML
<div *ngIf="canEdit()">
...
</div>
The above code snippet is where the function is being called
Blockquote Angular TS File
canEdit() {
let username = this.currentUser.user.username;
let values = {
"_id": this.org._id,
"user": username
}
return this.http.post<any>('http://localhost:3001/users/compareUser', values, { withCredentials: true }).subscribe(
result => {
if (result.message == "All good!") {
console.log(result);
return true;
} else {
return false;
}
}
)
Currently, when I run the code, it seems to be stuck in an infinite loop displaying {result: "All Good!"}. Interestingly, if I modify the canEdit() function to just return true and add a console log statement to track its execution count, it only runs once. However, with the existing code (TS), it does not seem to recognize the 'return true' statement that follows the console log.
Express route
router.post("/compareUser", function(req, res, next) {
let token = req.cookies.token;
let id = req.body._id;
let user = req.body.user;
if (token) {
Org.findById(id, (err, result) => {
if (err) {
console.log(err);
return res.status(401).json({
message: "Could not find Organization"
});
} else {
User.findOne({ username: user }).then(user => {
if (!user) {
return res.status(401).json({
message: "Are you sure you exist?"
});
} else {
if (user.username == result.username) {
return res.status(200).json({
message: "All good!"
});
}
}
});
}
});
} else {
return res.status(404).json({
message: "You must be logged in"
});
}
});