Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Asp.Net Core 2.1 with Angular 6 inside, Angular 6 service adding record using .net controller, what to return?, and use of MongoDB with auto increment

Asp.Net Core 2.1 with Angular 6 inside, Angular 6 service adding record using .net controller, what to return?, and use of MongoDB with auto increment

Scheduled Pinned Locked Moved ASP.NET
javascriptasp-netdatabasemongodbtutorial
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    Well I've made it this now, which is pretty good. I've almost completed the porting over the front end of my portfolio website and I'm working with the database now, in which I chose MongoDB since that seems to be the buzz word for job apps. A couple I don't understand about MongoDB. Guess the first question is the auto increment for the index column. Guess Mongo uses a unique identifier based on some other logic, so I used ObjectId.GenerateNewId(); Below is an example of my controller code which writes to Mongo successfully. see the CRMMessage; Q1: Should I just get rid of the "Id" which was int and is now string and just use InternalId? Or does MongoDB have the same mechanism that will auto increment? So before I added the the respository to the controller function, it worked fine. Angular would register a success and run my success code. But now it returns something I can't see, which results in Error 500 according to Chrome F12. Q2: Should I just change IActionResult to void and return nothing? I'm lost here on this.

    [HttpPost]
    public IActionResult AddMessage([FromBody] CRMMessage message)
    {
    if (message == null)
    {
    return BadRequest();
    }

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    
    \_crmRespository.AddMessage(new CRMMessage
    {
        // Id = "0",
        InternalId = ObjectId.GenerateNewId(),
        Name = message.Name,
        CompanyName = message.CompanyName,
        EmailAddress = message.EmailAddress,
        PhoneNumber = message.PhoneNumber,
        PhoneMobile = message.PhoneMobile,
        TimeZone = message.TimeZone,
        TimeStamp = message.TimeStamp,
        UpdatedOn = message.TimeStamp,
        Message = message.Message,
        RememberMe = message.RememberMe
    });
    
    return CreatedAtRoute("GetSingleMessage", new { id = message.Id }, message);
    

    }

    This is my Angular 6, TypeScript onSubmit Function. I know this question is in the wrong section bought thought I'd ask anyways since it tied together. I wonder how it knows the difference between a success and an error. There are tools like postman in which I need to learn how to use.:confused:

    onSubmit() {
    this.dataService.addMessage(this.model).subscribe(
    () => {

        this.messageSuccess = true;
        this.getStartedForm.reset();
    
        this.model.name = '';
        this.model.companyName = '';
        this.model.emailAddress = ''
        this.model.phoneNumber = '';
    
    J 1 Reply Last reply
    0
    • J jkirkerx

      Well I've made it this now, which is pretty good. I've almost completed the porting over the front end of my portfolio website and I'm working with the database now, in which I chose MongoDB since that seems to be the buzz word for job apps. A couple I don't understand about MongoDB. Guess the first question is the auto increment for the index column. Guess Mongo uses a unique identifier based on some other logic, so I used ObjectId.GenerateNewId(); Below is an example of my controller code which writes to Mongo successfully. see the CRMMessage; Q1: Should I just get rid of the "Id" which was int and is now string and just use InternalId? Or does MongoDB have the same mechanism that will auto increment? So before I added the the respository to the controller function, it worked fine. Angular would register a success and run my success code. But now it returns something I can't see, which results in Error 500 according to Chrome F12. Q2: Should I just change IActionResult to void and return nothing? I'm lost here on this.

      [HttpPost]
      public IActionResult AddMessage([FromBody] CRMMessage message)
      {
      if (message == null)
      {
      return BadRequest();
      }

      if (!ModelState.IsValid)
      {
          return BadRequest(ModelState);
      }
      
      \_crmRespository.AddMessage(new CRMMessage
      {
          // Id = "0",
          InternalId = ObjectId.GenerateNewId(),
          Name = message.Name,
          CompanyName = message.CompanyName,
          EmailAddress = message.EmailAddress,
          PhoneNumber = message.PhoneNumber,
          PhoneMobile = message.PhoneMobile,
          TimeZone = message.TimeZone,
          TimeStamp = message.TimeStamp,
          UpdatedOn = message.TimeStamp,
          Message = message.Message,
          RememberMe = message.RememberMe
      });
      
      return CreatedAtRoute("GetSingleMessage", new { id = message.Id }, message);
      

      }

      This is my Angular 6, TypeScript onSubmit Function. I know this question is in the wrong section bought thought I'd ask anyways since it tied together. I wonder how it knows the difference between a success and an error. There are tools like postman in which I need to learn how to use.:confused:

      onSubmit() {
      this.dataService.addMessage(this.model).subscribe(
      () => {

          this.messageSuccess = true;
          this.getStartedForm.reset();
      
          this.model.name = '';
          this.model.companyName = '';
          this.model.emailAddress = ''
          this.model.phoneNumber = '';
      
      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      Did some reading up on it and now it makes sense to me. Changed the id to InternalId that was generated by MongoDB.Bson and called GetSingleMessage to confirm the write, which results in a 201 code. This must be part of the CRUD operation procedure. I'll read up on the Id part, auto-increment for MongoDB. Then figure out if I need it the value of not, and whether it should be a string or int. Must admit it was exciting to see my angular app work correctly.

      var result = CreatedAtRoute("GetSingleMessage", new { id = message.InternalId }, message);
      return result;

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups