通过angularjs在mongodb中插入嵌入文档

时间:2016-02-05 18:02:12

标签: javascript angularjs node.js mongodb

我试图通过AngularJS在MongoDB中插入嵌入式文档。 父文档已存在。这是嵌入式文档的架构

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
    }],

这是我的路由器

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

在控制器中创建商品提供方法

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

这是来自AngularJS控制器的代码,插入了商品

$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/")
            });
        };

和AngularJS路由

$stateProvider
.state('offer', {
                url: "/:id/offer/",
                templateUrl: 'app/exposition/listOffers.tpl.html',
                controller: 'ExpositionsController'
            })

当我尝试插入优惠时,我收到了错误

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

我做错了吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

错误404它关于不存在这种情况下URL的资源来发帖子, 试试这条路线:

router.post( '/报价/:ID',expositionController.createOffer);

您也可以尝试使用get定义路径,仅通过GET /浏览器粘贴网址查看响应和访问资源:

router.get( '/报价/:ID',expositionController.createOffer);

如果你创建了这条路线,你只通过GET接收了属性:

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

你发票:     将此网址粘贴到您的浏览器中     http://localhost:3000/exposition/offer/0001

您可以将ID记录到预期:

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

};