这是我的User
模型定义
options: {
tableName: 'cushbu_users',
hooks:{},
timestamps:false,
/*--field names and validation rules --*/
email:{
type:Sequelize.STRING,
validate:{
notEmpty:true
}
},
first_name:{
type:Sequelize.STRING,
validate:{
notEmpty:true
}
}
}
如何在控制器中获取模型验证错误消息
我尝试了这些
User.validate().success(function () {
console.log('ok');
}).error(function (error) {
console.log(error);
})
但我收到了以下错误user.validate not function
答案 0 :(得分:1)
你应该从即时对象调用这个方法而不是像这样的续集模型:
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
namespace Common
{
public class QueueListener : ICommunicationListener
{
private readonly string _connectionString;
private readonly string _path;
private readonly Action<BrokeredMessage> _callback;
private QueueClient _client;
public QueueListener(string connectionString, string path, Action<BrokeredMessage> callback)
{
// Set field values
_connectionString = connectionString;
_path = path;
// Save callback action
_callback = callback;
}
public Task<string> OpenAsync(CancellationToken cancellationToken)
{
// Connect to subscription
_client = QueueClient.CreateFromConnectionString(_connectionString, _path);
// Configure the callback options
var options = new OnMessageOptions
{
AutoComplete = false
};
// Catch and throw exceptions
options.ExceptionReceived += (sender, args) => throw args.Exception;
// Wire callback on message receipt
_client.OnMessageAsync(message =>
{
return Task.Run(() => _callback(message), cancellationToken)
.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
message.CompleteAsync();
else
message.AbandonAsync();
}, cancellationToken);
}, options);
return Task.FromResult(_client.Path);
}
public Task CloseAsync(CancellationToken cancellationToken)
{
CloseClient();
return Task.FromResult(_client.Path);
}
public void Abort()
{
CloseClient();
}
private void CloseClient()
{
// Make sure client is still open
if (_client == null || _client.IsClosed)
return;
// Close connection
_client.Close();
_client = null;
}
}
}
答案 1 :(得分:0)
您可以在Model类中添加classMethods并将逻辑放在其中并返回错误。
classMethods: {
validator: function() {
var errors = [];
if (!this.email){
errors.push('Email id should not be null');
}
.
.
.
.
.
return errors;
},
}
从User.validator()
这样的路由调用方法,如果它返回空数组,则User
对象有效,否则显示错误消息。