javascript - Insert embedded document in mongodb via angularjs -
i trying insert embedded document in mongodb through angularjs. parent document existing. schema of embedded document
offers: [{ date: date, offer: { id: mongoose.schema.objectid, added: { type: date, default: date.now() }, displayname: string, creator: number, //creator: { // type: mongoose.schema.types.objectid, // ref: 'user' //}, photo: string, description: string, additional: { name: string, data: string } }, linkedby: number }],
this router
router.post('/',expositioncontroller.create); router.get('/',expositioncontroller.getall); router.get('/:id',expositioncontroller.get); router.put('/:id',expositioncontroller.update); router.delete('/:id',expositioncontroller.delete); router.post('/:id/offer',expositioncontroller.createoffer);
create offer method in controller
exports.createoffer = function(req,res){ var id = req.params.id; try{ id = new objectid(id); exposition.findbyid(id,function(err,exposition){ if(err){ res.send(err); } exposition.offer = new offer(req.body.offer); exposition.save(function(err){ if(err) res.send(err); res.json({message: "ok"}); }); }); }catch(e){ res.send(404); } };
here code angularjs controller inserting of offer
$scope.createoffer = function (_id) { var offerresource = new offerresource(); offerresource.offer = new offerupdateservice(); offerresource.offer.name = $scope.offer.name; offerresource.offer.photo = $scope.uploadphoto; offerresource.offer.description = $scope.offer.description; offerresource.$save(function (result) { $scope.offer.name = ''; $location.path("/exposition/") }); };
and angularjs routing
$stateprovider .state('offer', { url: "/:id/offer/", templateurl: 'app/exposition/listoffers.tpl.html', controller: 'expositionscontroller' })
when trying insert offer, got error
http://localhost:3000/exposition/offer 404 not found
whan doing wrong?
thanks!
error 404 not existe resource in case url's make post, try route:
router.post('/offer/:id',expositioncontroller.createoffer);
also may can try define rout get, view response , access resource via get/browser paste url:
router.get('/offer/:id',expositioncontroller.createoffer);
you received attributes via example if create route:
router.get('/offer/:id',expositioncontroller.createoffer);
you invoque: paste url in browser http://localhost:3000/exposition/offer/0001
and can log id expected:
exports.createoffer = function(req,res){ console.log(req.params.id) var id = req.params.id; try{ id = new objectid(id); exposition.findbyid(id,function(err,exposition){ if(err){ res.send(err); } exposition.offer = new offer(req.body.offer); exposition.save(function(err){ if(err) res.send(err); res.json({message: "ok"}); }); }); }catch(e){ res.send(404); }
};
Comments
Post a Comment