在REST API

时间:2015-10-15 18:25:33

标签: node.js mongodb rest mongoose

我正在尝试检索包含要在Web应用程序中列出的子文档的文档列表。

我的模型设置如下:

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

但是我得到了:

TypeError:undefined不是函数

填充这些子文件并返回要使用Web应用程序的评论列表的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

为什么你有: var Game?

应为 var Review