json - Sails.JS findOne 不返回关联

标签 json node.js rest sails.js waterline

我有一个函数可以根据地理坐标查找资源,但 findOne 方法不会从模型返回评论集合。如果我使用蓝图路线根据 ID 进行查找,我会得到注释。

查找: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'
    }

  }
};

最佳答案

您需要使用“填充”方法,如下所示:

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

关于json - Sails.JS findOne 不返回关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334949/

相关文章:

javascript - 如何在 NodeJS/JavaScript 中将文本格式 JSON 数组转换为 JSON 对象数组?

json - 在JSON中登录为JSON

node.js - 当 props.match.params.id 返回未定义但 id 在 url 上可见时,如何获取 url 的 id 参数?

node.js - 减少 NPM 内存使用

json - 如何在 Angular6 中从 JSON 文件获取日期值

javascript - Canvas/Fabric.js .loadFromJSON() 替换整个 Canvas

javascript - 在 JavaScript/Node.js 中思考 OOP

azure - 有没有办法在 Azure 数据工厂中使用 cURL 命令?

c# - 如果数据库中插入新记录,如何更新 SharePoint Online 列表?

c# - 使服务返回大量数据