2015-10-15 259 views
0

我正在嘗試檢索包含要在Web應用程序上列出的子文檔的文檔列表。在REST API中檢索包含子文檔的文檔

我有我的模式設置爲這樣:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var storeSchema = new mongoose.Schema({ 
    name: String, 
    address: String, 
    phone: String, 
    webUrl: String, 
    coords: {type: [Number], index: '2dsphere'} 
}); 

var reviewSchema = new mongoose.Schema({ 
    user: {type: String, required: true}, 
    store: { type: Schema.ObjectId, ref: 'Store' }, 
    review: {type: String}, 
    tags: [String] 
}); 

mongoose.model('Review', reviewSchema); 
mongoose.model('Store', storeSchema); 

和API控制器設置爲這樣:

var mongoose = require('mongoose'); 
var Game = mongoose.model('Review'); 

var sendJsonResponse = function(res, status, content) { 
res.status(status); 
res.json(content); 
}; 

module.exports.gamesListByDistance = function(req, res) { 
var lng = parseFloat(req.query.lng); 
var lat = parseFloat(req.query.lat); 
var maxDistance = parseFloat(req.query.maxDistance); 

var point = { 
    type: "Point", 
    coordinates: [lng, lat] 
}; 

var geoOptions = { 
    spherical: true, 
    maxDistance: theEarth.getRadsFromDistance(maxDistance), 
    num: 10 
}; 

Review.geoNear(point, geoOptions, function(err, results, stats) { 
    console.log('Geo Results', results); 
    console.log('Geo stats', stats) 
    if (err) { 
     console.log('geoNear error:', err); 
     sendJsonResponse(res, 404, err); 
    } else { 
     results.populate(results, {path:'store', select:'name coords'}, function(err,reviews) { 
      if (err) { 
       sendJsonResponse(res, 400, err); 
      } else { 
       games = buildReviewsList(req, res, results, stats); 
       sendJsonResponse(res, 200, reviews); 
      } 
     }); 

    } 
}); 
}; 

var buildReviewsList = function(req, res, results, stats) { 
    var reviews = []; 
    results.forEach(function(doc) { 
    reviews.push({ 
     distance: theEarth.getDistanceFromRads(doc.dis), 
     store: doc.obj.store.name, 
     status: doc.obj.status, 
     tags: doc.obj.tags, 
     _id: doc.obj._id 
    }); 
}); 
    return reviews; 
}; 

但我越來越:

類型錯誤:未定義是不是一個函數

什麼是填充這些subdocs並返回評論列表的正確方法是使用Web應用程序?

回答

0

爲什麼你有: var Game?

應該是var評論