An urgent guide request. Loopback,mongodb CURL implementation

Friends I need an urgent guide about loopback and mongodb, I had to build API with these tools. My problem is this, how to implemet cURL post method for example how should I implement some thing like this , in app.

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
  \"name\": \"GreatNight\",
  \"city\": \"Tehran\"
}" "http://localhost:3000/api/CoffeeShops"

to post data in to database.

this is its ajax but i dont know how to us it in app.

  type: 'POST',
  url: "http://localhost:3000/api/CoffeeShops",
  data: {
    name: "GreatNight",
    city: "Tehran"
  dataType: "json",
  accepts: {
    text: "application/json"

You want to use
(Note that this shows a wrong Angular version by default. You should see which Angular version you use and find the appropriate docs for that one.)

1 Like

this is the solution:


      <ion-label fixed> shop name </ion-label>
      <ion-input type="text"[(ngModel)]="name" > </ion-input>

      <ion-label fixed> city </ion-label>
      <ion-input type="text" [(ngModel)]="city"> </ion-input>

      <button ion-button block large (click)="post()">post  </button>




import { Component } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';

import { NavController, NavParams } from 'ionic-angular';

  selector: 'page-page2',
  templateUrl: 'page2.html'
export class Page2 {
  public name;
  public city;
  constructor( public navCtrl: NavController, public navParams: NavParams, public http: Http) {   }

let headers = new Headers(
  'Content-Type' : 'application/json'
let options = new RequestOptions({ headers: headers });

let data = JSON.stringify({,
let url = 'http://localhost:3000/api/CoffeeShops';
return new Promise((resolve, reject) => {, data, options)
  .then((response) =>
    console.log('API Response : ', response.json());
  .catch((error) =>
    console.error('API Error : ', error.status);
    console.error('API Error : ', JSON.stringify(error));


The above post has considerable room for improvement.

  • name and city should be typed
  • there is no need to explicitly declare them public, that is the default
  • there is no point in having post() return anything; nobody is listening
  • there is no need to make the headers or RequestOptions; Http will do this for you
  • similarly no need to stringify; simply pass the object as the body
  • even if we were wanting to return something, there is no need to explicitly instantiate a Promise
1 Like

@rapropos Thank you so much, I will correct them, now.