I'm create a ticket but i have problem when try send comment on ticket.
The ticket works, but comment on ticket does not work.
I'm using formBuild.
Follow mesage: Failed to load http://localhost:3977/api/tickets/comment: No 
'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:4200' is therefore not allowed access. The 
response had HTTP status code

My Code frontend:


// Function to display new ticket form
 newTicketForm() {
   this.newPost = true; // Show new ticket form
 }

  // Reload tickets on current page
 reloadTickets() {
   this.loadingTickets = true; // Used to lock button
   this.getTickets(); // Add any new tickets to the page
   setTimeout(() => {
     this.loadingTickets = false; // Release button lock after four seconds
   }, 4000);
 }

  // Function to post a new comment on ticket post
 draftComment(id) {
this.commentForm.reset(); // Reset the comment form each time users starts 
a new comment
   this.newComment = []; // Clear array so only one post can be commented 
on at a time
   this.newComment.push(id); // Add the post that is being commented on to 
the array

  }

  // Function to submit a new ticket post
 onTicketSubmit() {
   this.processing = true; // Disable submit button
   this.disableFormNewTicketForm(); // Lock form

    // Create ticket object from form fields
   const ticket = {
    title: this.form.get('title').value, // Title field
     body: this.form.get('body').value, // Body field
     createdBy: this.username // CreatedBy field
   }

    // Function to save ticket into database
   this._ticketService.newTicket(this.token, ticket ).subscribe(data => {
     // Check if ticket was saved to database or not
     if (!data.success) {
       this.messageClass = 'alert alert-danger'; // Return error class
       this.message = data.message; // Return error message
       this.processing = false; // Enable submit button
       this.enableFormNewTicketForm(); // Enable form
     } else {
       this.messageClass = 'alert alert-success'; // Return success class
       this.message = data.message; // Return success message
       this.getTickets();
       // Clear form data after two seconds
       setTimeout(() => {
         this.newPost = false; // Hide form
         this.processing = false; // Enable submit button
         this.message = false; // Erase error/success message
         this.form.reset(); // Reset all form fields
         this.enableFormNewTicketForm(); // Enable the form fields
       }, 2000);
     }
   });
 }

  // Function to go back to previous page
 goBack() {
   window.location.reload(); // Clear all variable states
 }

  // Function to get all tickets from the database
 getTickets() {
   // Function to GET all tickets from database
   this._ticketService.getTickets(this.token).subscribe(data => {
     this.ticketPosts = data.tickets; // Assign array to use in HTML
   });
 }
// Function to post a new comment
 postComment(id) {
   this.disableCommentForm(); // Disable form while saving comment to 
database
   this.processing = true; // Lock buttons while saving comment to database
   const comment = this.commentForm.get('comment').value; // Get the 
comment value to pass to service function
   // Function to save the comment to the database
     this._ticketService.postComment(this.token, comment).subscribe(data => 
{
     this.getTickets(); // Refresh all tickets to reflect the new comment
     const index = this.newComment.indexOf(id); // Get the index of the 
ticket id to remove from array
     this.newComment.splice(index, 1); // Remove id from the array
     this.enableCommentForm(); // Re-enable the form
     this.commentForm.reset(); // Reset the comment form
     this.processing = false; // Unlock buttons on comment form
     if (this.enabledComments.indexOf(id) < 0) this.expand(id); // Expand 
comments for user on comment submission
   
   });
 }

        return this._http.post(this.url+'tickets/comment', params, {headers: 
headers})
                                          .map(res => res.json());
      }


Back end:
function saveTicket(req, res){
if (!req.body.title) {
      res.json({ success: false, message: 'Ticket title is required.' }); 
// Return error message
    } else {
      // Check if ticket body was provided
      if (!req.body.body) {
        res.json({ success: false, message: 'Ticket body is required.' }); 
// Return error message
      } else {
       
          // Create the ticket object for insertion into database
          const ticket = new Ticket({
            title: req.body.title, // Title field
            body: req.body.body, // Body field
            createdBy: req.body.createdBy // CreatedBy field
          });
          ticket.save((err, ticketStored) => {
            // Check if error
            if (err) {
              // Check if error is a validation error
              if (err.errors) {
                // Check if validation error is in the title field
                if (err.errors.title) {
                  res.json({ success: false, message: 
err.errors.title.message }); // Return error message
                } else {
                  // Check if validation error is in the body field
                  if (err.errors.body) {
                    res.json({ success: false, message: 
err.errors.body.message }); // Return error message
                  } else {
                    res.json({ success: false, message: err }); // Return 
general error message
                  }
                }
              } else {
                res.json({ success: false, message: err }); // Return 
general error message
              }
            } else {
              res.json({ success: true, message: 'Ticket Salvo' }); // 
Return success message
            }
          });
        }
      }
    
};
function saveComment(req, res){
// Check if comment was provided in request body
    if (!req.body.comment) {
      res.json({ success: false, message: 'No comment provided' }); // 
Return error message
    } else {
      // Check if id was provided in request body
      if (!req.body.id) {
        res.json({ success: false, message: 'No id was provided' }); // 
Return error message
      } else {
        // Use id to search for ticket post in database
        Ticket.findOne({ _id: req.body.id }, (err, ticket) => {
          // Check if error was found
          if (err) {
            res.json({ success: false, message: 'Invalid ticket id' }); // 
Return error message
          } else {
            // Check if id matched the id of any ticket post in the database
            if (!ticket) {
              res.json({ success: false, message: 'Ticket not found.' }); 
// Return error message
            } else {
              // Grab data of user that is logged in
              User.findOne({ _id: req.decoded.userId }, (err, user) => {
                // Check if error was found
                if (err) {
                  res.json({ success: false, message: 'Something went 
wrong' }); // Return error message
                } else {
                  // Check if user was found in the database
                  if (!user) {
                    res.json({ success: false, message: 'User not found.' 
}); // Return error message
                  } else {
                    // Add the new comment to the ticket post's array
                    ticket.comments.push({
                      comment: req.body.comment, // Comment field
                      commentator: user.username // Person who commented
                    });
                    // Save ticket post
                    ticket.save((err, ticketStored) => {
                      // Check if error was found
                      if (err) {
                        res.json({ success: false, message: 'Something went 
wrong.' }); // Return error message
                      } else {
                        res.json({ success: true, message: 'Comment saved' 
}); // Return success message
                      }
                    });
                  }
                }
              });
            }
          }
        });
      }
    }
  };


//routes
api.post('/ticket',  TicketController.saveTicket);
api.post('/tickets/comment/',  TicketController.saveComment);



-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to