- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对如何管理水线的错误验证感到非常困惑,我需要一些关于良好实践的澄清。通常我有这样的 promise 链:
sails.models.user.findOne(...)
.then(function(){
//...
//...
return sails.models.user.update(...);
})
.then(....)
.then(....)
.catch(function(err){
})
出现的一个问题是当水线返回验证错误时。在这种情况下,我通常需要知道问题何时是由客户端的错误输入或代码中的错误产生的。
我最终所做的是将水线 promise 包装在一个正确处理验证错误的 promise 中。所以最终的代码是:
...
.then(function(){
//...
//...
return new Promise(function(resolve,reject){
sails.models.user.update(...)
.then(resolve)
.catch(function(err){
//the error is a bug, return the error object inside the waterline WLError
reject(err._e);
//the error is caused by wrong input, return the waterline WLError
reject(err);
})
})
})
.then(function(){
//second example: we are sure that a validation error can't be caused by a wrong input
return wrapPromise(sails.models.user.find());
})
.then(....)
.catch(function(err){
//WLError ---> res.send(400);
//Error object --> res.send(500);
})
function wrapPromise(action){
//return an error object on validation error
return new Promise(function(resolve,reject){
action
.then(resolve)
.catch(function(err){
reject(err._e || err);
})
})
}
我做事正确吗?有没有更好的方法来正确处理错误?谢谢
最佳答案
您只需在 catch 中添加一个检查即可区分验证错误和其他错误:
sails.models.user.findOne(...)
.then(function(){
//...
//...
return sails.models.user.update(...);
})
.then(....)
.then(....)
.catch(function(err){
if(err.error == "E_VALIDATION") {
// validation error
}
})
关于node.js - sails.js/waterline 验证错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446967/
每当我启动 SailsJS 时,我都会收到此错误: Error: Cannot find module 'sails/node_modules/waterline/lib/waterline/quer
您好,我对 Sails 和 Nodejs 很陌生, 我使用 mysql 作为数据库。 我收到这个错误 var schemaAttributes = this.waterline.schema[this
我只是想知道是否可以使用水线选择特定字段,下面给出了orientdb查询。 e.g. select phone from user 我想使用此查询从用户顶点选择手机 userModel.find(ph
标题非常简单。我有一个如下所示的属性: isSet: { type: boolean, defaultsTo: false } 所以我的目标是在每次创建用户时将值设置为
我有一个包含 a、b、c、d 列的表格。但我只想向浏览器发送来自 a、b 的数据。我发现可以重写 toJSON() 方法,但在我看来这是 1 行结果的解决方案。如何使它适用于 json 数组? 我可以
我正在调用 REST API 来更新 sails 中的数据库条目。因此,我有一个函数,它迭代 JSON 数组并为每个条目调用另一个函数,该函数将值保存到数据库中。 function getData()
我有一个具有以下属性的模型: attributes: { title: { type: 'string' }, tags: { type:
如图stackoverflow answer由于 Waterline 不支持级联(特别是级联删除),因此可以通过使用 afterDestroy (或 afterUpdate 用于软删除)生命周期回调并
sails/waterline 是否提供了一种声明方式来强制我的模型中的数组元素为字符串类型?还是我必须编写代码来执行此验证? 最佳答案 这似乎可以通过向模型添加自定义类型来实现(在 Waterlin
在我的 nodejs 应用程序(使用 sailsjs)中,我使用 Controller 接收以 .zip 格式上传的多个图像,然后使用水线将它们保存在我的数据库中: var images = [];
我想知道是否可以在 Waterline 中定义模型或按名称获取模型,如 Node-ORM2 . 定义: var Person = db.define("person", { name
我正在尝试在表字段 (MySQL) 中插入一个由 node JS 缓冲区对象 表示的文件(在 waterline js), 当读取它并将其写入文件时,文件已损坏,我实现了一个 java 程序来更新包含
Sails 的 Waterline 允许您将实体的属性声明为“数组”类型: module.exports = { attributes: { stuff: { type: 'array'
我遇到一个问题,我的模型是使用 boolean 类型的属性定义的,但在某些时候它被解释为字符串。 例如,模型如下所示: { attributes: { id: 'string' b
Waterline ORM 是否支持继承?比如说,我有一个名为 BusinessDocument 的模型,我想创建一个名为 SalesOrder 的新模型来扩展 BusinessDocument。因此
我有一个表 user_address,它有一些字段,例如 attributes: { user_id: 'integer', address: 'string' //etc. } 目前我这样做
Token.create({ type: 'invite-into-org', email: email, initiator: organization,
Waterline 文档给出了使用 beforeCreate 的示例散列密码。除非您对密码字段进行验证并尝试更新记录,否则效果很好。这是我截取的示例: types: { hasUpperCas
我正在为模型服务提供者构建测试计划,并且我想在测试之前加载测试数据(仅使用内存适配器)(相对简单),但是有没有一种快速而肮脏的方法来截断测试后的所有集合每个测试套件都完成了吗? 提前致谢。 最佳答案
如何告诉我的 sails 模型我想要 type: 'float' 属性具有一些特定的小数位?像 decimalPlaces: 4 或类似的东西? 问题是,当我向此条目发布值时,磁盘上的值被截断为 .0
我是一名优秀的程序员,十分优秀!