2017-03-04 48 views
0

我是使用貓鼬,bodymen和express的yeoman的generator-rest生成器,我更新了生成器中提供的默認用戶架構。Cast for Array失敗值「[object Object] 」路徑爲

當我從郵遞員發送請求我收到錯誤Cast to Array failed for value \"[object Object]\" at path \"corp_locations\"

試圖在多個帖子所有可能的方式後,我不能夠解決這個錯誤。

這裏是我的架構

const locationsSchema = new Schema({ 
    location_name: String, 
    address: String, 
    area: String, 
    pincode: Number 
}) 

const userSchema = new Schema({ 
    user_id: { 
    type: String, 
    required: [true, 'User Id is required.'], 
    unique: true, 
    trim: true, 
    match: [/^\d{10}$/, 'User Id is invalid.'] 
    }, 
    password: { 
    type: String, 
    required: [true, 'Password is required.'], 
    minlength: 6 
    }, 
    email: { 
    type: String, 
    match: [/^\[email protected]\S+\.\S+$/, 'Email is invalid.'], 
    required: [true, 'Email is required.'], 
    unique: true, 
    trim: true, 
    lowercase: true 
    }, 
    corp_locations: [locationsSchema], 
    created_by: String, 
    updated_by: String 
}, { 
    timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } 
}) 

POST方法

router.post('/', 
    master(), 
    body(schema.tree) 
    create) 

控制器的創建方法

export const create = ({ bodymen: { body } }, res, next) => 
    User.create(body) 
    .then((user) => user.view(true)) 
    .then(success(res, 201)) 
    .catch((err) => { 

     res.status(409).json(validationMessages(err)) 
    }) 

這裏是郵差

{ 
    "user_id" : "1000000002", 
    "password" : "password", 
    "email" : "[email protected]", 
    "corp_locations" : [ 
     { 
      "location_name": "loc1", 
      "address": "arrd1", 
      "area": "area1", 
      "pincode": "9092230" 
     } 
    ] 
} 

送請讓我知道我做錯了的JSON。

回答

2

經過了很多谷歌,我發現這一點。

https://github.com/diegohaz/bodymen/issues/1

正是有了bodymen問題和POST方法應該是這樣的。

router.post('/', 
    master(), 
    body({user_id, email, password, user_type, corp_locations: [Object]}), 
    create) 

schema.tree工作正常進行簡單的文件,但對嵌套文件,我們必須分別指定領域,很多不必要的代碼,但是這是如何工作的。

相關問題