JillAndMe
JillAndMe

Reputation: 4561

How can I post array in nested object on Postman/form-data?

I want to post loc data in postman/form-data, without application/json header.

{
    "text": "abcd",
    "loc":  {
                "type": "Point",
                "coordinates": [-80, 25]
             }
    "img": multipart
}

When I treid this in postman/form-data

loc.coordinates[0]: -80 loc.coordinates[1]: 25 text: ... img: ...

loc.coordinates returns nothing see below.

{
    "data": {
        "text": "test",
        "tags": [
            "testtest"
        ],
        "_id": "5b9e1462c1f13a0fe0912933",
        "img": "https://seoul-image-server.s3.cp.ap-northeast-2.amazonaws.com/1537086562695.png",
        "user": "5b8e4249f9f8184d0c825c6a",
        "date": "2018-09-16T08:29:22.791Z",
        "__v": 0
    }
}

When I tired this in form-data

loc: {"type": "Point", "coordinates": [-80, 25]} ...

It returns

 "message": "Memo validation failed: loc: Cast to Embedded failed for value \"{\"type\": \"Point\", \"coordinates\": [-80, 25]}\" at path \"loc\""

Why??

Here is my schema

const geoSchema = new Schema({
  type: {
    type: String,
    default: 'Point'
  },
  coordinates: {
    required: true,
    type: [Number]
  }
});

const memoSchema = new Schema({
  img: {
    type: String,
    required: true
  },
  text: {
    type: String,
    default: ""
  },
  date: {
    type: Date,
    default: Date.now
  },
  tags: {
    type: [String],
    set: item => {
      if(Array.isArray(item)) {
        return item.join('')
      }
    },
    defulat: []
  },
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  loc: geoSchema
});

memoSchema.index({
  loc: '2dsphere'
});

module.exports = mongoose.model('Memo', memoSchema)

Upvotes: 0

Views: 2482

Answers (1)

Lazar Nikolic
Lazar Nikolic

Reputation: 4404

You are sending JSON which means you have to wrap an array inside double quotes "coordinates": "[-80, 25]"

Upvotes: 1

Related Questions