2017-01-22 51 views
0

我有一個叫做藤貓鼬模型,看起來像這樣:中值數組傳遞到郵政快遞航線

content: [{ 
    contentType: { 
     type: String, 
     default: null 
    }, 
    contentValue: { 
     type: String, 
     default: null 
    } 
}] 

我的郵政快遞的路線是這樣的:

app.post('/todos', authenticate, (req, res) => { 
    var todo = new Todo({ 
     content: req.body.content 

    }); 
    res.send(todo) 
    //I am sending instead of sending the result for testing 
}); 

當我使用Postman發送int測試數據內容數組將返回空「內容」:[]

我已經嘗試了幾種格式的郵差包括:

{ 
    "content[contentType]": "reminder", 
    "content[contentValue]": "Will it blend" 
} 

{ 
    "content": { 
     "contentType": "reminder", 
     "contentValue": "Will it blend" 
    } 
} 

但是都回來與空數組。

{ 
    "content": [] 
} 

我是否需要更改我的POST路由和/或以另一種格式發送數據?這裏

+0

我想你是不是叫todo.save() –

回答

0

內容是待辦事項子文件,所以你應該把它像下面這樣:

app.post('/todos', authenticate, (req, res) => { 
    var content = req.body.content; 
    var todo = new Todo(); 

    todo.content.push(content); 

    todo.save(function(err) { 
     if (err) throw err; 
     res.json(todo.toJSON()) 
     //I am sending instead of sending the result for testing 
    }); 

}); 

參見:Mongoose SubDocuments

+0

這是正確的答案然而作爲一個子文檔,它不會自動生成一個ObjectID。我需要手動生成並保存嗎? – dwax

+0

你期望什麼和現在產生什麼? – dNitro

+1

我的錯誤 - 它按預期工作。 – dwax