Sails ReferenceError:文章"模型"没有定义

时间:2015-04-14 16:10:36

标签: javascript node.js sails.js waterline

我尝试通过我的控制器中的文章模型访问findOne(),但没有成功。

我有这个错误

>  error: Sending 500 ("Server Error") response: 
>      ReferenceError: Article is not defined
>         at Object.module.exports.createArticle (c:\Users\pisix\BizBiz\BizBizBackEnd\api\controllers\ArticleController.js:16:9)
>         at bound (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\lodash\dist\lodash.js:729:21)
>         at routeTargetFnWrapper (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:179:5)
>         at callbacks (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:164:37)
>         at param (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:138:11)
>         at param (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:135:11)
>         at pass (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:145:5)
>         at nextRoute (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:100:7)
>         at callbacks (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:167:11)
>         at C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:187:7
>         at alwaysAllow (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\lib\hooks\policies\index.js:207:11)
>         at routeTargetFnWrapper (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:179:5)
>         at callbacks (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:164:37)
>         at param (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:138:11)
>         at param (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:135:11)
>         at pass (C:\Users\pisix\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:145:5)
> [ReferenceError: Article is not defined]

我该如何解决?

在我的文章模型下面

/**
* Article.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false,
    tableName:'article',
    identity:'article',
    attributes: {
        idArticle:{
            type:'integer',
            unique:true,
            required:true,
            columnName:'IDARTICLE',
            autoIncrement:true,
            primaryKey:true
        },
        titre:{
            type:'string',
            required:true,
            columnName:'TITRE',
            index:true
        },
        details:{
            type:'text',
            required:true,
            columnName:'DETAILS',
            size:100
        },
        etat:{
            type:'string',
            columnName:'ETAT',
            enum:['Vendu','Réservé'],
            index:true
        },

        prix:{
            type:'float',
            columnName:'PRIX',
            index:true,
            required:true
        },
        dateAjout:{
            type:'datetime',
            columnName:'DATEAJOUT',
            index:true,
            required:true
        },
     }
};

在我的控制器ArticleController.js下面

module.exports = {

    /**
     * `ArticleController.createArticle()`
     */
    createOArticle: function (req, res) {
        var idarticle = req.body.idarticle;
        console.log('idarticle to edit: '+ idarticle);
        Article.findOne({idArticle:idarticle}).exec(function (err, article) {
            if (article) {
                console.log('article '+ idarticle + ' trouvé');
                var dataarticle = req.body.article;
                article.titre = dataarticle.titre;
                article.details = dataarticle.details;
                article.prix = dataarticle.prix;
                article.etat = dataarticle.etat;
                article.dateAjout=new Date();

                article.save(function (err, success) {
                    if (success) {
                        return res.send({articleupdate: true, article: article,success:true,message:req.__('message-success-creation')});
                    }
                    if (err) {
                        return res.send({success: false, message:req.__('message-erreur-update')})
                    }
                });
            }
            if (err)
            {
                //Create Article
            }

        });
    },

};

我正在使用风帆v0.11

3 个答案:

答案 0 :(得分:2)

您不必定义模型的标识。 Sails.js将它用作身份并将其分配给全球我认为。

只需删除模型的以下行

即可
identity:'article',

答案 1 :(得分:1)

检查您的模型文件夹它可能在Controller文件夹中。我已经面临同样的问题了。我不知道Models文件夹是如何插入Controller文件夹的。所以请检查您的Controller文件夹。你可能有同样的问题。

答案 2 :(得分:0)

我尝试将名称更改为较低的字母,例如。

        company.find({}).exec(function(err, items) {

它对我有用。