首页 > 解决方案 > SailsJS 蓝图失控

问题描述

我一直在使用 SailsJS v0.12 开发一个应用程序。该应用程序按预期工作。但是,对于某些模块,升级到 v1.0 是有意义的,因为它提供了一些很棒的功能,并且网站暗示(或看起来如此)升级过程很简单。

事实并非如此。经过一天繁琐的重构后,我成功构建了现有代码并开始着手开发新模块。需要关注的问题从这里开始。

我像以前在POSTman中一样运行了这些功能,但我停止了响应。为了检查单个模型,我尝试使用蓝图。由于没有对任何模型进行结构更改,这应该有效。但我无法在 POST 中得到任何回复;GET 仍在工作。我注销休息一下,然后向具有某些关联的模型发出 GET 请求(之前工作完美)。

我收到了这个回复

UsageError:无效的填充。customer详细信息:由于用法不明确,无法填充 。这是一个单数(“模型”)关联,这意味着它永远不会引用多个关联记录。因此,此关联不支持传入子标准(即作为 的第二个参数 .populate()),因为它通常没有任何意义。但这就是问题所在——看起来提供了某种子标准(或其他东西!(请注意,子标准仅包含omitselect是一种确实有意义的特殊情况。这种用法将在未来版本的 Waterline 中得到支持。)

以下是传入的内容: { limit: 30 }

完整的 JSON 响应是:

{
    "stack": "UsageError: Invalid populate(s).\nDetails:\n  Could not populate `customer` because of ambiguous usage.  This is a singular (\"model\") association, which means it never refers to more than _one_ associated record.  So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association, since it generally wouldn't make any sense.  But that's the trouble-- it looks like some sort of a subcriteria (or something) _was_ provided!\n(Note that subcriterias consisting ONLY of `omit` or `select` are a special case that _does_ make sense.  This usage will be supported in a future version of Waterline.)\n\nHere's what was passed in:\n{ limit: 30 }\n\n    at findRecords (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\hooks\\blueprints\\actions\\find.js:40:21)\n    at routeTargetFnWrapper (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\router\\bind.js:181:5)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:164:37)\n    at param (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:138:11)\n    at pass (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:145:5)\n    at nextRoute (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:100:7)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:167:11)\n    at alwaysAllow (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\hooks\\policies\\index.js:224:11)\n    at routeTargetFnWrapper (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\router\\bind.js:181:5)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:164:37)\n    at param (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:138:11)\n    at pass (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:145:5)\n    at nextRoute (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:100:7)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:167:11)\n    at module.exports (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\hooks\\cors\\clear-headers.js:14:3)\n    at routeTargetFnWrapper (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\router\\bind.js:181:5)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:164:37)\n    at param (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:138:11)\n    at pass (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:145:5)\n    at nextRoute (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:100:7)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:167:11)\n    at sails.router.bind._middlewareType (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\hooks\\csrf\\index.js:148:11)\n    at routeTargetFnWrapper (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\router\\bind.js:181:5)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:164:37)\n    at param (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:138:11)\n    at pass (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:145:5)\n    at nextRoute (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:100:7)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:167:11)\n    at _addResViewMethod (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\hooks\\views\\res.view.js:372:3)\n    at routeTargetFnWrapper (D:\\Dev\\NodeJS\\zingr\\node_modules\\sails\\lib\\router\\bind.js:181:5)\n    at callbacks (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:164:37)\n    at param (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:138:11)\n    at pass (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:145:5)\n    at nextRoute (D:\\Dev\\NodeJS\\zingr\\node_modules\\express\\lib\\router\\index.js:100:7)",
    "message": "Invalid populate(s).\nDetails:\n  Could not populate `customer` because of ambiguous usage.  This is a singular (\"model\") association, which means it never refers to more than _one_ associated record.  So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association, since it generally wouldn't make any sense.  But that's the trouble-- it looks like some sort of a subcriteria (or something) _was_ provided!\n(Note that subcriterias consisting ONLY of `omit` or `select` are a special case that _does_ make sense.  This usage will be supported in a future version of Waterline.)\n\nHere's what was passed in:\n{ limit: 30 }\n",
    "name": "UsageError",
    "code": "E_INVALID_POPULATES",
    "details": "Could not populate `customer` because of ambiguous usage.  This is a singular (\"model\") association, which means it never refers to more than _one_ associated record.  So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association, since it generally wouldn't make any sense.  But that's the trouble-- it looks like some sort of a subcriteria (or something) _was_ provided!\n(Note that subcriterias consisting ONLY of `omit` or `select` are a special case that _does_ make sense.  This usage will be supported in a future version of Waterline.)\n\nHere's what was passed in:\n{ limit: 30 }"
}

令人困惑的是,这只发生在本地。蓝图在 Zeit.co 上托管的部署(相同的代码,双重检查)上按预期完美运行。我已经测试了足够多的时间来推断这不是我的模型的错误。尽管如此,这里是模型 -BillCustomer

models\Bill.js

module.exports = {

    primaryKey:'id',
    attributes: {
        createdAt: { type: 'number', autoCreatedAt: true, },
        updatedAt: { type: 'number', autoUpdatedAt: true, },

        id:{
            type:'string',
            required:true
        },
        bill_number:{
            type:'string'
        },
        admin_id:{
            type:'string'
        },
        channel:{
            type:'string'
        },
        amount:{
            type:'number'
        },
        discount:{
            type:'number',
            defaultsTo:0
        },
        order_date:{
            type: 'number'
        },
        status:{
            type:'string',
            isIn:['generated', 'settled', 'canceled'],
            defaultsTo:'generated'
        },
        type:{
            type:'string',
            isIn:['dine-in', 'take-away', 'delivery', 'logistics-delivery'],
            required:true
        },
        logisticsPartner:{
            type:'string',
            defaultsTo:'phone'
        },

        //association
        customer:{
            model:'customer'
        },

        store:{
            model:'store'
        },
        orderContents:{
            collection:'billmapping',
            via:'bill'
        },
        transactions:{
            collection:'transaction',
            via:'bill'
        }
    }

};

models\Customer.js

module.exports = {

    attributes: {
        createdAt: { type: 'number', autoCreatedAt: true, },
        updatedAt: { type: 'number', autoUpdatedAt: true, },

        id: { type: 'number', autoIncrement: true}, // <-- for SQL databases
        name:{
            type:'string'
        },
        number:{
            type:'string',
            unique:true
        },
        email:{
            type:'string',
            unique:true
        },
        age:{
            type:'number'
        }
    }
};

这个问题已经让我把头发拉了 8 个小时。任何人都可以阐明或分享一些建议吗?

提前致谢。

标签: mysqlsails.jswaterline

解决方案


推荐阅读