如何将 $http.Post 值赋给 Mongoose Schema 中的子文档?
How to $http.Post the Values to a Subdoucment of Mongoose Schema?
我是 Mongoose 和 Mongo 的新手:-
var Tag = new Schema({pno: NUMBER,_id:false,id:false});
// Myshcema
var mySchema= new Schema({
Name: {
type: String,
default: '',
trim: true,
required: ' Name cannot be blank'
},
phones:[Tag ],
email:String,
services: [Tag],
address :{
city : String,
zip : Number,
loc:[string]
}
});
Angular UI:-
<
div class="form-group">
<label class="col-sm-2 control-label" for="pno">Phone Number</label>
<div class="controls"><div class="col-sm-3">
<input name="pno" data-ng-model="pno" id="pno" class="form-control" placeholder="pno"></input>
</div></div>
</div>
POST 方法:-
var resObj = {
Name: this.Name,
address{
city:this.city, // Fields from address sub document
zip :this.zip // Fields from address sub document
},
phones:[this.pno]// Actually itis not working
};
Post i used from Client side :-
var res = $http.post('http://localhost:3000/service', resObj);
从服务器端保存Api的方法:-
exports.create = function(req, res) {
Var newapi= new Service(_.extend(req.body));
newapi.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(newapi);
}
});
};
但是当我发布成功时...结果并不好..
Result :-
{"_id":"6b01c0e00b2e7b3",Name:'service name',**"phones":[null],"services":[]**,"address":{"loc":[]}}
**address.city is not Filling the Value**
如何填写 **Phones , service 以及我们如何 Post Sub文档值 ?**
老实说,我对此一无所知...请有人帮助我回答...非常感谢!!!
phones 字段是一个数组,您需要 post 一个与您的 Service
架构匹配的对象,即更改您创建 resObj
对象的方式:
var resObj = {
Name: this.Name,
address: {
city: this.city,
zip: this.zip
},
phones: [
this.pno
]
}
我是 Mongoose 和 Mongo 的新手:-
var Tag = new Schema({pno: NUMBER,_id:false,id:false});
// Myshcema
var mySchema= new Schema({
Name: {
type: String,
default: '',
trim: true,
required: ' Name cannot be blank'
},
phones:[Tag ],
email:String,
services: [Tag],
address :{
city : String,
zip : Number,
loc:[string]
}
});
Angular UI:- <
div class="form-group">
<label class="col-sm-2 control-label" for="pno">Phone Number</label>
<div class="controls"><div class="col-sm-3">
<input name="pno" data-ng-model="pno" id="pno" class="form-control" placeholder="pno"></input>
</div></div>
</div>
POST 方法:-
var resObj = {
Name: this.Name,
address{
city:this.city, // Fields from address sub document
zip :this.zip // Fields from address sub document
},
phones:[this.pno]// Actually itis not working
};
Post i used from Client side :-
var res = $http.post('http://localhost:3000/service', resObj);
从服务器端保存Api的方法:-
exports.create = function(req, res) {
Var newapi= new Service(_.extend(req.body));
newapi.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(newapi);
}
});
};
但是当我发布成功时...结果并不好..
Result :-
{"_id":"6b01c0e00b2e7b3",Name:'service name',**"phones":[null],"services":[]**,"address":{"loc":[]}}
**address.city is not Filling the Value**
如何填写 **Phones , service 以及我们如何 Post Sub文档值 ?** 老实说,我对此一无所知...请有人帮助我回答...非常感谢!!!
phones 字段是一个数组,您需要 post 一个与您的 Service
架构匹配的对象,即更改您创建 resObj
对象的方式:
var resObj = {
Name: this.Name,
address: {
city: this.city,
zip: this.zip
},
phones: [
this.pno
]
}