Sails.js / Waterline - 创建不是模型上的功能

Posted

技术标签:

【中文标题】Sails.js / Waterline - 创建不是模型上的功能【英文标题】:Sails.js / Waterline - Create is not a function on model 【发布时间】:2017-06-22 00:28:26 【问题描述】:

我是 Node.js 和 Sails.js 框架的新手。我正在尝试使用该框架构建一个网站/应用程序,但无法让模型部分按预期工作。我已经看了很多文档,但仍然无法执行此基本任务。我正在使用 PostgreSQL 适配器。

堆栈如下:

TypeError: Fragrance.create is not a function\n    at Object.module.exports.index (/home/kelly/workspace/Hawthorne-Acres/api/controllers/HomeController.js:16:19)\n    at wrapper (/usr/local/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19)\n    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:164:37)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:138:11)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:135:11)\n    at pass (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:145:5)\n    at nextRoute (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:100:7)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:167:11)\n    at alwaysAllow (/usr/local/lib/node_modules/sails/lib/hooks/policies/index.js:224:11)\n    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:164:37)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:138:11)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:135:11)\n    at pass (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:145:5)\n    at nextRoute (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:100:7)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:167:11)\n    at module.exports (/usr/local/lib/node_modules/sails/lib/hooks/cors/clear-headers.js:14:3)\n    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:164:37)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:138:11)\n    at pass (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:145:5)"

Fragrance.js(位于 api/models 文件夹中):

module.exports = 

    attributes: 
        fragranceName: 
            type: 'string',
            required: true
        ,

        listings: 
            collection: "listing",
            via: "fragrance"
        
    
;

调用函数的控制器

    /**
 * HomeController
 *
 * @description :: Server-side logic for managing Homes
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */
var Fragrance = require("../models/Fragrance");

module.exports = 



    /**
     * `HomeController.Index()`
     */
    index: function(req, res) 
        Fragrance.create( fragranceName: 'Vanilla' ).exec(function(err, rec) 
            console.log(rec.id);
        );
        return res.render("index",  title: "Welcome" );

    ,


    /**
     * `HomeController.About()`
     */
    about: function(req, res) 
        console.log("About place");
        return res.render("about",  title: "About" );
    ,


    /**
     * `HomeController.Contact()`
     */
    contact: function(req, res) 
        return res.render("contact",  title: "Contact" );
    
;

这可能是显而易见的,但我已经努力自己弄清楚了,但没有成功。因此,我们将不胜感激。

谢谢。

【问题讨论】:

【参考方案1】:

model 文件夹中的模型全局可用。您可以禁用config/globals.js 中的功能。所以你可以放弃

var Fragrance = require("../models/Fragrance");

然后您可以通过以下方式创建记录:

Fragrance.create 方法

sails.models.fragrance.create 方法(注意模型名称是小写的。)

就个人而言,我更喜欢第二种模式,并在全球范围内关闭模型的可用性。

【讨论】:

非常感谢您的回答和基本解释;它现在正在工作。

以上是关于Sails.js / Waterline - 创建不是模型上的功能的主要内容,如果未能解决你的问题,请参考以下文章

Sails.js/waterline: groupBy + count

Sails.js Waterline ORM:.findOrCreate() 没有 .populate() 方法

Sails.js 1.0:如何在没有Waterline的情况下连接和使用旧版MySQL数据库?

在视图中显示一对多关系sails.js waterline orm的列表

Sails.Js - 我如何在sails.Js 中进行分页

Sails.js 使用sails-mysql 的日期的水线查询修饰符?