帆参考错误:未定义第 "Model" 条



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

我有这个错误

>  error: Sending 500 ("Server Error") response: 
>      ReferenceError: Article is not defined
>         at Object.module.exports.createArticle (c:UserspisixBizBizBizBizBackEndapicontrollersArticleController.js:16:9)
>         at bound (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_moduleslodashdistlodash.js:729:21)
>         at routeTargetFnWrapper (C:UserspisixAppDataRoamingnpmnode_modulessailslibrouterbind.js:179:5)
>         at callbacks (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:164:37)
>         at param (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:138:11)
>         at param (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:135:11)
>         at pass (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:145:5)
>         at nextRoute (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:100:7)
>         at callbacks (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:167:11)
>         at C:UserspisixAppDataRoamingnpmnode_modulessailslibrouterbind.js:187:7
>         at alwaysAllow (C:UserspisixAppDataRoamingnpmnode_modulessailslibhookspoliciesindex.js:207:11)
>         at routeTargetFnWrapper (C:UserspisixAppDataRoamingnpmnode_modulessailslibrouterbind.js:179:5)
>         at callbacks (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:164:37)
>         at param (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:138:11)
>         at param (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.js:135:11)
>         at pass (C:UserspisixAppDataRoamingnpmnode_modulessailsnode_modulesexpresslibrouterindex.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
        },
     }
};

在我的控制器文章下面控制器.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

您不必定义模型的标识。Sails.js将把它作为一个身份,并将其分配给全球。

只需删除模型的以下行

identity:'article',

我试图将名称更改为小写字母,例如。

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

它对我有用。

检查您的模型文件夹 它可能在控制器文件夹中。 我已经面临同样的问题。我不知道模型文件夹是如何插入控制器文件夹的。因此,请检查您的控制器文件夹。您可能有同样的问题。

相关内容

  • 没有找到相关文章

最新更新