Field word "location" causes nodejs server to crash

Object {errors: Object, _message: “Treasure validation failed”, message: “Treasure validation failed: location: Cast to Numb… failed for value “Testing me” at path “location””, name: “ValidationError”}
errors
:
Object
message
:
"Treasure validation failed: location: Cast to Number failed for value “Testing me” at path “location”"
name
:
“ValidationError”
_message
:
“Treasure validation failed”

Below is my debug code. If I change location to anything else, for example, _location

  save(treasure){
 
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    let treasure = {
      title: "Gold",
      location: "Testing me"
    };
 
    this.http.post('http://10.0.0.82:8080/add/treasure', JSON.stringify(treasure), {headers: headers})
      .subscribe(res => {
        console.log(res.json());
      }), (err)=> { 
      	 //console.log(err)
      };
 
  }

The error message seems pretty straightforward to me. You are passing a string “Testing me” to something that is expecting a number instead, so it pukes.

Incidentally, you do not need to define that header object or manually call JSON.stringify(). Http handles both of those things.

Why would “location” expect a number? Is it because the database already has a old record with the location field being a number? I will try deleting all records and rerun with “testing me”.

I use stringify and header because I am following a tutorial. I shall read more on this subject. The JSON conversation back and forth is driving me crazy :frowning:

You’re going to have to ask whoever wrote the backend.

I would not consider it a very good one.

Thank you @rapropos
The back-end is like this. No wonder!!!.. I was scratching my head all day long …

var Treasure = mongoose.model('Treasure', {
    title: String,
    description: String,
    location: Number
});

I was reading things on http, it says http only handles plain text, and JSON.stringify is used to convert an object to strings. If I don’t do manual JSON.stringify() call, what is the right way to do it?

Simply pass the object as the body. When Http sees that it’s an object, it will handle both stringification and content type for you.

There’s a difference between http and Http. If you’re using the Angular Http provider, it’s best to read that official documentation. The web is full of Ionic tutorials that are either out of date or just not very good.