【问题标题】:mongoose population of documents from other collections来自其他集合的文件的猫鼬种群
【发布时间】:2017-07-26 13:28:55
【问题描述】:

我正在尝试在我的 API 中在 RetailersGeofencePoints 之间创建某种关系。一个零售商对象应该有一个地理围栏点列表。我尝试关注官方文档:http://mongoosejs.com/docs/populate.html。当我执行查询为零售商放置地理围栏位置时,我收到了 http 200 响应,但是当我通过 id 获取 Retail 对象时,geofencePoints 列表仍然为空。我究竟做错了什么?这是我的代码:

路线

    app.route('/geofencePoints')
    .get(geofencePointController.GET)
    .post(geofencePointController.POST)
    .delete(geofencePointController.DELETE)

    app.route('/geofencePoints/:point_id')
    .get(geofencePointController.GETid)
    .put(geofencePointController.PUTid)
    .delete(geofencePointController.DELETEid);

    app.route('/retailers')
    .get(retailerController.GET)
    .post(retailerController.POST);

    app.route('/retailers/:retailer_id')
    .get(retailerController.GETid)
    .put(retailerController.PUTid)
    .delete(retailerController.DELETEid);

    app.route('/retailers/:retailer_id/geofencePoints')
    .put(geofencePointController.PUTgeofencesForRetailId);

geofencePointController.js

var GeofencePoint = require('../model/geofencePoint');
var Retailer = require('../model/retailer');

exports.GET = function (req, res) {
    GeofencePoint.find(function (err, points) {
        if (err)
            res.send(err);
        res.json(points);
    });
};

exports.POST = function (req, res) {
    var geofencePoint = new GeofencePoint();
    geofencePoint.name = req.body.name;
    geofencePoint.latitude = req.body.latitude;
    geofencePoint.longitude = req.body.longitude;
    geofencePoint.radius = req.body.radius;
    geofencePoint.save(function (err) {
        if (err)
            return res.json({ success: false, msg: 'Name already exists.' });
        res.json({ success: true, msg: 'Successful created new geofence.' });
    });
};

exports.DELETE = function (req, res) {
    GeofencePoint.remove({
    }, function (err, point) {
        if (err)
            res.send(err);
        res.json({ message: 'Successfully deleted all' });
    });
};

exports.GETid = function (req, res) {
    GeofencePoint.findById(req.params.point_id, function (err, point) {
        if (err)
            res.send(err);
        res.json(point);
    });
};

exports.PUTid = function (req, res) {
    GeofencePoint.findById(req.params.point_id, function (err, point) {
        if (err)
            res.send(err);
        point.name = req.body.name;
        point.latitude = req.body.latitude;
        point.longitude = req.body.longitude;
        point.radius = req.body.radius;
        point.save(function (err) {
            if (err)
                res.send(err);
            res.json({ message: 'Geofence location updated!' });
        });
    });
};

exports.DELETEid = function (req, res) {
    GeofencePoint.remove({
        _id: req.params.point_id
    }, function (err, point) {
        if (err)
            res.send(err);
        res.json({ message: 'Successfully deleted' });
    });
};

//===================================================================
//  JOINED DATA
//===================================================================

exports.PUTgeofencesForRetailId = function (req, res) {
    Retailer.find({}).populate(req.params.retailer_id).exec(function (err, geofencePoint) {
            if (err) return handleError(err);
        var geofencePoint = new GeofencePoint();
        geofencePoint.name = req.body.name;
        geofencePoint.latitude = req.body.latitude;
        geofencePoint.longitude = req.body.longitude;
        geofencePoint.radius = req.body.radius;
        geofencePoint.save(function (err) {
            if (err) return res.json({ success: false, msg: 'Something went wrong' });
        res.json({ success: true, msg: 'Success' });
        });
    });
};

retailerController.js

var Retailer = require('../model/retailer');

exports.GET = function (req, res) {
    Retailer.find(function (err, retailers) {
        if (err)
            res.send(err);
        res.json(retailers);
    });
};
exports.GETid = function (req, res) {
    Retailer.findById(req.params.retailer_id, function (err, retailer) {
        if (err)
            res.send(err);
        res.json(retailer);
    });
};
exports.POST = function (req, res) {
    var retailer = new Retailer();
    retailer.name = req.body.name;

    retailer.save(function (err) {
        if (err)
            return res.json({ success: false, msg: 'Name already exists.' });
        res.json({ success: true, msg: 'Successful created new retailer.' });
    });
};
exports.PUTid = function (req, res) {
    Retailer.findById(req.params.retailer_id, function (err, retailer) {
        if (err)
            res.send(err);
        retailer.name = req.body.name;

        retailer.save(function (err) {
            if (err)
                res.send(err);
            res.json({ message: 'Retailer updated!' });
        });
    });
};
exports.DELETEid = function (req, res) {
    Retailer.remove({
        _id: req.params.point_id
    }, function (err, retailer) {
        if (err)
            res.send(err);
        res.json({ message: 'Successfully deleted' });
    });
};

retailer.js

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

var retailerSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    mail: {
        type: String,
    },
    telephone: {
        type: String,
    },
    street: {
        type: String,
    },
    housenumber: {
        type: String,
    },
    postalCode: {
        type: String,
    }, 
    city: {
        type: String,
    },
    slogan: {
        type: String,
    },
    geofencePoints : [{ 
        type: Schema.Types.ObjectId, 
        ref: 'GeofencePoint' }]
});

module.exports = mongoose.model('Retailer', retailerSchema);

geofencePoint.js

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

var pointSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    latitude: {
        type: Number,
        required: true
    },
    longitude: {
        type: Number,
        required: true
    },
    radius: {
        type: Number,
        required: true
    },
    duration: {
        type: Number,
    },
});

module.exports = mongoose.model('GeofencePoint', pointSchema);

我希望有人能解释我做错了什么。谢谢

【问题讨论】:

    标签: node.js mongodb express mongoose mongoose-populate


    【解决方案1】:

    您需要在 Retailer 文档中保存对新创建的 GeofencePoint 的引用。
    另外,我不明白为什么您在进行更新时尝试填充 Retailer(我认为您尝试填充错误,此处填充的唯一元素确实是 geofencePoint,而不是 Retailers)。

    exports.PUTgeofencesForRetailId = function (req, res) {
    
        var geofencePoint = new GeofencePoint({
            name: req.body.name,
            latitude: req.body.latitude,
            longitude: req.body.longitude,
            radius: req.body.radius
        });
    
        geofencePoint.save(function (err, geofencePoint) {
            if (err) return res.json({ success: false, msg: 'Something went wrong' });
            Retailer.findById(req.params.retailer_id, function (err, retailer) {
                if (err) return handleError(err);
                retailer.geofencePoints.push(geofencePoint._id);
                retailer.save(function (err, retailer) {
                    if (err) return handleError(err);
                    res.json({ success: true, msg: 'Success' });
                });
            });
        });
    };
    

    根据您的应用,当然有更好/更简洁的方法,但它提供了一个想法。

    【讨论】:

    • 感谢输入。你是对的,它现在正在工作。 “有更好的方法”是什么意思?
    • 很高兴它的工作原理! “更好的方法”是指避免过多回调的人,可能使用 PromiseAsync.js librairie。
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 2016-12-17
    • 2018-07-18
    • 2019-09-13
    • 2017-06-02
    • 2016-07-11
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多