2017-03-06 1 views
1

私のAPIには、RetailersGeofencePointsの間に何らかの関係を作成しようとしています。小売業者オブジェクトにはジオフェンスポイントのリストが必要です。私は公式の文書に従うことを試みた:http://mongoosejs.com/docs/populate.html。私は小売業者のジオフェンスの場所をPUTするためにクエリを実行するときに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); 

。 THX

答えて

0

あなたの小売文書に新規作成したGeofencePointへの参照を保存する必要があります。
また、私はあなたが更新を行うときに小売業者に人口を入れようとしている理由を理解していません。あなたはそれを間違って入れようとしていると思うのですが、ここに居住する唯一の要素は小売業者ではなくgeofencePointです。

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

確かに、あなたのアプリに応じて、より良い/より簡潔なアプローチがありますが、それはアイデアを提供します。

+0

入力用のThx。あなたは正しいと思って今働いています。 「よりよいアプローチがある」とはどういう意味ですか? – Tim

+0

これはうまくいきました! 「より良いアプローチ」とは、あまりにも多くのコールバックを避け、おそらく_Promise_または_Async.js_ librairieを使用することを意味します。 – TGrif

関連する問題