Sails.js中的模型验证失败

时间:2014-11-17 01:44:42

标签: javascript node.js model sails.js waterline

我测试了Sails.js中的以下Employee.js模型,我发现了一些我觉得难以理解的东西。

当发布创建新员工的表单时,我在Employee.js模型的第一个定义中遇到以下错误,但Employee.js的第二个定义有效。那么Employee.js的两个定义之间的区别是什么?

错误:

Error (E_VALIDATION) :: 3 attributes are invalid
    at WLValidationError.WLError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:33:18)
    at new WLValidationError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js:20:28)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:45:43
    at allValidationsChecked (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:195:5)
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:186:14
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:157:64

Invalid attributes sent to Employee:
 • name
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null
 • email
   • `undefined` should be a email (instead of "null", which is a object)

   • "required" validation rule failed for input: null
 • password
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null

定义1:Employee.js

module.exports = {
  attributes: {

    name: {
      type: 'STRING',
    },

    email: {
      type: 'STRING',
      email: true,
    },

    password: {
      type: 'STRING',
    },

    toJSON: function() {
      var obj = this.toObject();

      return {
        name: obj.name,
        email: obj.email,
        password: obj.password
      }
    }

  }
};

定义2:Employee.js

module.exports = {
  attributes: {

    name: 'STRING',
    email: 'STRING',
    password: 'STRING',

    toJSON: function() {
      var obj = this.toObject();

      return {
        name: obj.name,
        email: obj.email,
        password: obj.password
      }
    }

  }
};

创建新员工的表单如下:

<form action="/signupemployee" method="POST">
  <table>
    <tr><td>Name</td><td><input type=”text” name=”name”></td></tr>
    <tr><td>Password</td><td><input type=”password” name=”password”></td></tr>
    <tr><td>Email</td><td><input type=”email” name=”email”></td></tr>
    <tr><td></td><td><input type="submit"></td>
  </table>
</form>

2 个答案:

答案 0 :(得分:0)

只有一个区别是 存在。在您的第二个模型中,email属性不需要包含有效电子邮件格式的字符串。据我所知,定义如:

name: {
    type: "STRING"
}

name: "STRING"

意图是等效的,所以这可能是Waterline中的一个错误。

虽然这个问题在不久前被问过,但仍然值得提出一个问题以确保它得到解决。

答案 1 :(得分:0)

这应该有效:

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void invia(byte[] bytetosend)
        {
            // Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                // This example uses port 11000 on the local computer.
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);

                // Connect the socket to the remote endpoint. Catch any errors.
                try
                {
                    sender.Connect(remoteEP);

                    Console.WriteLine("Socket connected to {0}",
                        sender.RemoteEndPoint.ToString());

                    // Encode the data string into a byte array.
                    var mahByteArray = new List<byte>();
                    mahByteArray.AddRange(bytetosend);
                    string eof = "<EOF>";
                    mahByteArray.Insert(0, Convert.ToByte(eof)); // Adds eof bytes to the beginning.
                    byte[] msg = mahByteArray.ToArray();

                    // Send the data through the socket.
                    int bytesSent = sender.Send(msg);

                    // Receive the response from the remote device.
                    int bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Echoed test = {0}",
                        Encoding.ASCII.GetString(bytes, 0, bytesRec));

                    // Release the socket.
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();

                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }


        public void ricevi()
        {
            // Data buffer for incoming data.
            List<byte> bytes = (new Byte[1024]).ToList();
            List<byte> scambioArray = new List<byte>();

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the 
            // host running the application.
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and 
            // listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = listener.Accept();
                    string eofr = "<EOF>";
                    List<byte> trova = Encoding.UTF8.GetBytes(eofr).ToList();

                    // An incoming connection needs to be processed.
                    while (true)
                    {
                        bytes = (new byte[1024]).ToList();
                        int bytesRec = handler.Receive(bytes.ToArray());


                        if (bytes.Contains(trova[0]))
                        {
                            List<byte> mahByteArray = new List<byte>();
                            mahByteArray.AddRange(bytes);
                            mahByteArray.Remove(trova[0]);
                            bytes = mahByteArray.ToArray().ToList();
                            break;
                        }
                        scambioArray.AddRange(bytes);
                    }

                    // Echo the data back to the client.
                    byte[] msg = scambioArray.ToArray();
                    MemoryStream stream = new MemoryStream(msg);    
                    MessageData.Image = Image.FromStream(stream);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }

}
​