2

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!

1 Answer 1

2

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);
}

};

Sign up to request clarification or add additional context in comments.

4 Comments

Uhhm, but where would I get exposition._id?
in this case you received params only via URL, and post send params via request object
Ok, but in the url you metioned there is no exposition._id
eeeh if it would be a little earlier... I have rethink my structure and did it as you mentioned. Anyway, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.