error: Sending 500 ("Server Error") resp-6ren">
gpt4 book ai didi

javascript - 风 sails 引用错误 : Article "Model" is not defined

转载 作者:搜寻专家 更新时间:2023-11-01 00:42:36 26 4
gpt4 key购买 nike

我尝试通过 Controller 中的 Article 模型访问 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
},
}
};

在我的 Controller 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
}

});
},

};

我正在使用 sails v0.11

最佳答案

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

只需删除模型的以下行

identity:'article',

关于javascript - 风 sails 引用错误 : Article "Model" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29632288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com