通过angularjs在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

我做错了什么?

谢谢!

mottaman85

错误404及其有关在这种情况下URL的资源不存在,请尝试以下路线:

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

也可以尝试用get定义一个路由,仅通过GET /浏览器粘贴url查看响应和访问资源:

router.get('/ offer /: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);
}

};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

查询MongoDB中的嵌入式文档

来自分类Dev

在mongodb中展开嵌入式文档

来自分类Dev

将嵌入式文档插入到mongodb文档中的新字段

来自分类Dev

如何在OrientDB中插入嵌入式文档

来自分类Dev

从mongodb中的嵌入式文档中删除元素

来自分类Dev

如何在嵌入式数组mongodb中查询嵌入式文档

来自分类Dev

MongoDB $ n在嵌入式文档的某些字段中

来自分类Dev

在MongoDB中更新完整的嵌入式文档

来自分类Dev

在mongodb中更新嵌套的嵌入式文档

来自分类Dev

在Mongodb中的嵌入式文档上设置索引

来自分类Dev

查询mongodb中的嵌入式文档数组

来自分类Dev

映射-Symfony2中的Mongodb嵌入式文档

来自分类Dev

嵌入式文档数组中的MongoDB Aggregate ObjectId

来自分类Dev

在mongodb中更新嵌套的嵌入式文档

来自分类Dev

映射-Symfony2中的Mongodb嵌入式文档

来自分类Dev

使用python(pymongo)在mongodb中编辑嵌入式文档

来自分类Dev

如何在 MongoDb 中查询嵌入式文档?

来自分类Dev

将对象插入嵌入式文档

来自分类Dev

是否有任何查询通过检查mongoDb中嵌套的嵌入式文档的字段来查找文档

来自分类Dev

mongodb汇总嵌入式文档值

来自分类Dev

如何验证mongodb嵌入式文档

来自分类Dev

mongodb汇总嵌入式文档值

来自分类Dev

返回特定的mongodb嵌入式文档

来自分类Dev

MongoDb $ match用于嵌入式文档

来自分类Dev

何时使用嵌入式文档MongoDB

来自分类Dev

mongodb嵌入式文档搜索

来自分类Dev

在文档中引用文档的嵌入式数组的嵌入式数组上查询Doctrine2和MongoDB

来自分类Dev

为什么我的MongoDb查询在Update上插入嵌入式文档?

来自分类Dev

如何使用Spring Data MongoDB MongoTemplate插入嵌入式文档

Related 相关文章

热门标签

归档