【问题标题】:Sails.JS findOne not returning AssociationSails.JS findOne 不返回关联
【发布时间】:2015-06-02 19:17:28
【问题描述】:

我有一个函数可以根据它的地理坐标查找资源,但是 findOne 方法不会从模型中返回 cmets 集合。如果我使用蓝图路由根据 ID 查找,我会得到 cmets。

查找:ID JSON

{
    "comments": [
        {
            "text": "Some Text",
            "nomination": "551865064a5ccf41274be682",
            "createdAt": "2015-03-29T21:02:10.586Z",
            "updatedAt": "2015-03-29T21:02:10.586Z",
            "id": "55186852d21850a627292db3"
        }
    ],
    "name": "Karma Bird House",
    "address": "47 Maple Street Burlington, VT 05401",
    "geo": "44.473231,-73.217882",
    "lat": 44.473231,
    "lon": -73.217882,
    "streetImg": "http://maps.googleapis.com/maps/api/streetview?size=800x300&location=44.473231,-73.217882",
    "votes": 1,
    "createdAt": "2015-03-29T20:48:06.109Z",
    "updatedAt": "2015-03-29T21:02:48.817Z",
    "id": "551865064a5ccf41274be682"
}

findGeo:id JSON

{
    "nom": {
        "name": "Karma Bird House",
        "address": "47 Maple Street Burlington, VT 05401",
        "geo": "44.473231,-73.217882",
        "lat": 44.473231,
        "lon": -73.217882,
        "streetImg": "http://maps.googleapis.com/maps/api/streetview?size=800x300&location=44.473231,-73.217882",
        "votes": 1,
        "createdAt": "2015-03-29T20:48:06.109Z",
        "updatedAt": "2015-03-29T21:02:48.817Z",
        "id": "551865064a5ccf41274be682"
    }
}

findGeo 方法

findGeo: function (req, res, next) {
    Nomination.findOne({geo: req.param('id')}, function foundNomination(err, nom) {
        if(err) return next(err);
        if(!nom) return next();

        return res.json({
            nom: nom
        });
    })
}

型号:

module.exports = {

  attributes: {
    id:{
        type:"int",
        primaryKey: true,
        autoIncrement: true
    },
    // Location's name
    name: {
        type: 'string',
        required: true
    },

    // Locations Lat and lon
    lat: 'float',

    lon: 'float',

    // lat,lon
    geo: {
        type: 'string',
        unique: true
    },

    // Location's Address
    address: {
        type: 'string',
        required: true
    },

    // Number of Votes
    votes: {
        type: 'integer',
        defaultsTo: '0'
    },

    // Street View Image
    streetImg: 'string',

    // Association of comments
    comments: {
        collection: 'comment',
        via: 'nomination'
    }

  }
};

【问题讨论】:

    标签: json node.js rest sails.js waterline


    【解决方案1】:

    您需要像这样使用“填充”方法:

    findGeo: function (req, res, next) {
        Nomination
        .findOne({geo: req.param('id')})
        .populate('comments')
        .exec(function (nom) {
            if(!nom) return next();
            return res.json({
                nom: nom
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 2019-02-13
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多