2014-10-08 122 views
0

我在获取模型中的嵌套对象时遇到了问题。 我有一家模特餐厅,我参考了一篇文章。这只是一个演示,我来测试,这是如何工作的,但我没有得到它.. :(使用MongoosJS,NodeJS和AngularJS填充嵌套对象

啊..我用meanjs ..

这里是我的餐厅模型..

'use strict'; 

/** 
* Module dependencies. 
*/ 
var mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 

/** 
* Restaurant Schema 
*/ 
var RestaurantSchema = new Schema({ 
    name: { 
     type: String, 
     default: '', 
     required: 'Please fill Restaurant name', 
     trim: true 
    }, 
    desc: { 
     type: String, 
     default: 'description is here' 
    }, 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    user: { 
     type: Schema.ObjectId, 
     ref: 'User' 
    }, 
    article: { 
     type: Schema.ObjectId, 
     ref: 'Article' 
    } 
}); 

mongoose.model('Restaurant', RestaurantSchema); 

这是我第模型

'use strict'; 

/** 
* Module dependencies. 
*/ 
var mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 

/** 
* Article Schema 
*/ 
var ArticleSchema = new Schema({ 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    title: { 
     type: String, 
     default: '', 
     trim: true, 
     required: 'Title cannot be blank' 
    }, 
    content: { 
     type: String, 
     default: '', 
     trim: true 
    } 
}); 

mongoose.model('Article', ArticleSchema); 

这两种方法都在后台的NodeJS餐厅控制器:

exports.list = function(req, res) { Restaurant.find().sort('-created').populate('article').exec(function(err, restaurants) { 
     if (err) { 
      return res.status(400).send({ 
       message: errorHandler.getErrorMessage(err) 
      }); 
     } else { 
      res.jsonp(restaurants); 
     } 
    }); 
}; 

exports.restaurantByID = function(req, res, next, id) { Restaurant.findById(id).populate('article').exec(function(err, restaurant) { 
     if (err) return next(err); 
     if (! restaurant) return next(new Error('Failed to load Restaurant ' + id)); 
     req.restaurant = restaurant ; 
     next(); 
    }); 
}; 

然后,我有一个角度控制器的方法来安全的新餐厅和文章,这是工作。当我访问"http://localhost:3000/#!/articles"时。但是,当我试图拿出例如标题,它不起作用。

我创造我的餐厅和文章这样说:

// Create new Restaurant 
     $scope.create = function() { 
      // Create new Restaurant object 

      var newArticle = new Articles({ 
       title: this.articleTitle 
      }); 
      newArticle.$save(); 

      var restaurant = new Restaurants({ 
       name: this.name, 
       desc: this.description, 
       article: newArticle._id 
      }); 

      // Redirect after save 
      restaurant.$save(function (response) { 
       $location.path('restaurants/' + response._id); 

       // Clear form fields 
       $scope.name = ''; 
      }, function (errorResponse) { 
       $scope.error = errorResponse.data.message; 
      }); 
     }; 

这是我创造的观点:

<div class="controls"> 
         <input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required> 
         <input type="text" data-ng-model="articleTitle" id="article" class="form-control" placeholder="artikel" required> 
         <input type="text" data-ng-model="description" id="description" class="form-control" placeholder="desription" required> 
        </div> 

,这是在我的列表视图

<h4 class="list-group-item-heading" data-ng-bind="restaurant.name"></h4> 
      <h3 class="list-group-item-heading" data-ng-bind="restaurant.article"></h3> 
      <h3 class="list-group-item-heading" data-ng-bind="restaurant.desc"></h3> 

的描述在浏览器中可见,但对文章没有任何可见的地方。我如何获得有关餐馆对象中文章的信息?

也许,这是很容易的,但我没有发现它。在先进

感谢..

+0

Interresting的事情也是,我没有得到文章的ID或者.. – damir 2014-10-08 15:50:30

回答

0

,所以我想通了..我怎么做我制作的一个错误文章。我thougt也许这是错的文章,然后我创建了一个新的crud菜..

首先,我保存这道菜,并在回调我创建餐厅与我从回调响应中获得的信息。 。这个工作对我来说..

// Create new Restaurant 
     $scope.create = function() { 
      // Create new Restaurant object 

      var dishId = 0; 
      var thisObject = this; 

      var newDish = new Dishes({ 
       name: this.dishName 
      }); 
      newDish.$save(function(response){ 
        console.log('XX in $save: %s', response._id); 
        dishId = response._id; 

        var restaurant = new Restaurants({ 
         name: thisObject.name, 
         desc: thisObject.description, 
         art: { 
          title: thisObject.artTitle, 
          content: thisObject.artContent 
         }, 
         dish: dishId 
        }); 

        // Redirect after save 
        restaurant.$save(function (response) { 
         $location.path('restaurants/' + response._id); 

         // Clear form fields 
         $scope.name = ''; 
        }, function (errorResponse) { 
         $scope.error = errorResponse.data.message; 
        }); 
       } 
      ); 
     }; 

,当然还有,我在我的服务器控制器来填充这个新菜:

exports.list = function(req, res) { Restaurant.find().sort('-created').populate('dish').exec(function(err, restaurants) { 
     if (err) { 
      return res.status(400).send({ 
       message: errorHandler.getErrorMessage(err) 
      }); 
     } else { 
      res.jsonp(restaurants); 
     } 
    }); 
}; 

exports.restaurantByID = function(req, res, next, id) { Restaurant.findById(id).populate('dish').exec(function(err, restaurant) { 
     if (err) return next(err); 
     if (! restaurant) return next(new Error('Failed to load Restaurant ' + id)); 
     req.restaurant = restaurant ; 
     next(); 
    }); 
}; 

,这是我的店型号:

'use strict'; 

    /** 
    * Module dependencies. 
    */ 
    var mongoose = require('mongoose'), 
     Schema = mongoose.Schema; 

    /** 
    * Restaurant Schema 
    */ 

var RestaurantSchema = new Schema({ 
    name: { 
     type: String, 
     default: '', 
     required: 'Please fill Restaurant name', 
     trim: true 
    }, 
    desc: { 
     type: String, 
     default: 'description is here' 
    }, 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    user: { 
     type: Schema.ObjectId, 
     ref: 'User' 
    }, 
    article: { 
     type: Schema.ObjectId, 
     ref: 'Article' 
    }, 
    dish: { 
     type: Schema.ObjectId, 
     ref: 'Dish' 
    }, 
    art: { 
     title: { 
      type: String, 
      default: 'HelloTitle', 
      trim: true, 
      required: 'Title cannot be blank' 
     }, 
     content: { 
      type: String, 
      default: '', 
      trim: true 
     } 
    } 
}); 

mongoose.model('Restaurant', RestaurantSchema);