Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

nothing happens when i submit a form in MEAN STACK

so the project is so simple. the user inputs some infos about a book and click submit to save the infos in a database (Mongodb) i’m using MEAN STACK and i’m a noobie (obviously). nothing happens when i submit not even an error.

backend code :

    //POST ONE BOOK
    booksRoute.route('/post').post((req,res,next)=>{
        var x = {
            name   : req.params.name,
            genre  : req.params.genre,   
            author : req.params.author,
            rating : req.params.rating,
            price  : req.params.price
        }
        console.log(x) ; 
        BookModel(x).save((err,data)=>{
            if (err){console.log(err);}
            else res.send(data) ;
}) ;
})

add book component html :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<ul>
    <form #f="ngForm" (ngSubmit)="onSubmit(f)">  
    <li>
    <label>book's name *</label>
    <input name="name" type="text" ngModel>
    </li>
    <li>
    <label>book's genre *</label>
    <input name="genre" type="text" ngModel>
    </li>
    <li>
    <label>book's author *</label>
    <input name="author" type="text" ngModel>
    </li>
    <li>
    <label>rating *</label>
    <input name="rating" type="number" [max]="10" min="0" ngModel>
    </li>
    <li>
    <label>price *</label>
    <input name="price" type="number" ngModel>
    <input class="btn btn-primary" type="submit"/> 
    </li>  
</form>
</ul>

angular service

  postBook(data){
    console.log(data) ;
    return this.http.post(this.backendUrl+'/post',data) ;
  }

in addBook component ts

onSubmit(f){
    //console.log(f.value) ;
    this.apiService.postBook(f.value) ; 
  }

Note : i can get data from the database, i can find a book by it’s name but i can’t post

>Solution :

I guess that You need to subscribe to the response of your API/back, something like this:

onSubmit(f){
    //console.log(f.value) ;
    this.apiService.postBook(f.value)
    .subscribe( res => {
     console.log('The back response:', res);
    }); 
}

Until you don’t subscribe to the observable which has the http call to the API, is not going to "start" the http call process.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading