Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to insert embedded document in MongoDB through AngularJS. Parent document is existing. This is 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 is my 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 is code from AngularJS controller with 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 I am trying to insert an offer, I got an error

http://localhost:3000/exposition/offer 404 not found

Whan am I doing wrong?

Thanks!

share|improve this question
up vote 2 down vote accepted

error 404 its about not existe the resource in this case URL's to make a post, try with this route:

router.post('/offer/:id',expositionController.createOffer);

also may you can try define a rout with get, only to view the response and access resource via GET/browser paste url:

router.get('/offer/:id',expositionController.createOffer);

you only received attributes via GET for example if you create this route:

router.get('/offer/:id',expositionController.createOffer);

you invoque: paste this url in your browser http://localhost:3000/exposition/offer/0001

and you can log the id to 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);
}

};

share|improve this answer
    
Uhhm, but where would I get exposition._id? – Sanat Serikuly 17 hours ago
    
in this case you received params only via URL, and post send params via request object – mottaman85 17 hours ago
    
Ok, but in the url you metioned there is no exposition._id – Sanat Serikuly 17 hours ago
    
im updated the original answer i hope this help you – mottaman85 15 hours ago
    
eeeh if it would be a little earlier... I have rethink my structure and did it as you mentioned. Anyway, thank you! – Sanat Serikuly 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.