node.js - Sails 0.10 & 多对多关联 - 获取所有关联数据,而不仅仅是第一个

标签 node.js sails.js waterline

我有 2 个具有多对多关联的模型。我关联这两个模型的代码运行良好(它创建了一个新的集合 item_languages__language_items ,其中包含相应的文档)。但随后我很难获取特定项目的所有关联数据(语言)。我正在使用 MongoDB。

// Item.js
module.exports = {
    schema: true,
    autoPK: false,
    attributes: {
        uuid: {
            type: 'string',
            primaryKey: true,
            unique: true,
            required: true,
            uuidv4: true
        },
        languages: {
            collection: 'language',
            via: 'items',
            dominant: true
        }
    }
}

// Language.js
module.exports = {
    schema: true,
    autoPK: false,
    attributes: {
        code: {
            type: 'string',
            primaryKey: true,
            required: true,
            minLength: 2,
            maxLength: 2,
            unique: true
        },
        items: {
            collection: 'item',
            via: 'languages'
        }
    }
}

存储在item_languages__language_items集合中的数据:

/* 0 */
{
    "language_items" : "es",
    "item_languages" : "69e4f3a3-1247-4a06-ae2d-9df27ac9495b",
    "_id" : ObjectId("5330bcebf8e0b61509c771d5")
}

/* 1 */
{
    "language_items" : "fr",
    "item_languages" : "69e4f3a3-1247-4a06-ae2d-9df27ac9495b",
    "_id" : ObjectId("5330bd26f8e0b61509c771d6")
}

/* 2 */
{
    "language_items" : "en",
    "item_languages" : "69e4f3a3-1247-4a06-ae2d-9df27ac9495b",
    "_id" : ObjectId("5330bedcc076355b09da3ccd")
}

现在在我的 ItemController.js 中,我想获取具有所有关联语言的特定项目:

Item
    .findOne({uuid: '69e4f3a3-1247-4a06-ae2d-9df27ac9495b'})
    .populate('languages')
    .exec(function (e, r) {
        console.log(r.toJSON());
    });

但是当我期望获得 3 种关联语言时,我在这里得到的项目只有 1 种关联语言。

最佳答案

这似乎是 sails-mongo 当前测试版实现中的一个错误,它使 populate 无法使用自定义键正常工作。请post this to the sails-mongo issues forum !与此同时,唯一的解决方案似乎是使用默认的 MongoDB 主键。

关于node.js - Sails 0.10 & 多对多关联 - 获取所有关联数据,而不仅仅是第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623226/

相关文章:

mysql - 使用 Cloud SQL 在 Google App Engine 上扬 sails 起航

node.js - Sails.js 在给定时间或每天结束时使 session 过期

javascript - sails js 上的自定义和多语言消息错误如何解决

node.js - 如何从 XML Node 集文件生成服务器地址空间?

javascript - 如何在正则表达式中使用模板文字?

node.js - 多部分/表单数据到 azure blob

javascript - Emscripten:调用修改数组元素的 C 函数

javascript - 从文件中定义变量

node.js - 在集合/对象中查找 - SailsJS 和 Waterline ORM

foreign-keys - 水线关联,更改外键?