How to validate string length with Mongoose?

Shamoon

My validation is:

LocationSchema.path('code').validate(function(code) {
  return code.length === 2;
}, 'Location code must be 2 characters');

as I want to enforce that the code is always 2 characters.

In my schema, I have:

var LocationSchema = new Schema({
  code: {
    type: String,
    trim: true,
    uppercase: true,
    required: true,
  },

I'm getting an error: Uncaught TypeError: Cannot read property 'length' of undefined however when my code runs. Any thoughts?

Jean-Philippe Leclerc

The field "code" is validated even if it is undefined so you must check if it has a value:

LocationSchema.path('code').validate(function(code) {
  return code && code.length === 2;
}, 'Location code must be 2 characters');

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to get length of a string using strlen function

来自分类Dev

How do I get the length of the first line in a multi line string?

来自分类Dev

compute length string of variable with cobol

来自分类Dev

How to use HList to validate input?

来自分类Dev

How ISo day works in mongoose?

来自分类Dev

Calculate the length of a string in a specific file format with perl

来自分类Dev

string to array, max length, break on words

来自分类Dev

Pulling variable length substring from middle of string

来自分类Dev

mysql to append a number of stars based on the string length

来自分类Dev

打印string.length时出错

来自分类Dev

将std :: string :: length转换为int

来自分类Dev

C ++ string.length()奇怪的行为

来自分类Dev

std :: string length()函数如何工作?

来自分类Dev

在变更集中的多个字段上应用Ecto validate_length

来自分类Dev

How to calculate length of nested JSON?

来自分类Dev

How to show / validate error of a node listing page

来自分类Dev

java return String[] in method,String[].length 不正确

来自分类Dev

使用String.length()和string.toCharArray()。length进行速度/效率权衡

来自分类Dev

目标C中string.length和[string length]之间的差异

来自分类Dev

How to assign callback return value to a variable in Mongoose?

来自分类Dev

How to format Mongoose debug output - pretty print

来自分类Dev

mongoose: how to return documents not Cursor with .find()

来自分类Dev

无法获取=== string.length才能正常工作

来自分类Dev

在Java中,将String :: length用于Comparator.comparing()

来自分类Dev

Fixed length, 6-bit binary string in perl

来自分类Dev

Order by on length of the string and then by single alphabet desc in pl sql

来自分类Dev

MongoDB Length of String or need help in mongoDB mapReduce query

来自分类Dev

Index and length must refer to a location within the string error in substring

来自分类Dev

String.length如何在JavaScript中工作?

Related 相关文章

  1. 1

    How to get length of a string using strlen function

  2. 2

    How do I get the length of the first line in a multi line string?

  3. 3

    compute length string of variable with cobol

  4. 4

    How to use HList to validate input?

  5. 5

    How ISo day works in mongoose?

  6. 6

    Calculate the length of a string in a specific file format with perl

  7. 7

    string to array, max length, break on words

  8. 8

    Pulling variable length substring from middle of string

  9. 9

    mysql to append a number of stars based on the string length

  10. 10

    打印string.length时出错

  11. 11

    将std :: string :: length转换为int

  12. 12

    C ++ string.length()奇怪的行为

  13. 13

    std :: string length()函数如何工作?

  14. 14

    在变更集中的多个字段上应用Ecto validate_length

  15. 15

    How to calculate length of nested JSON?

  16. 16

    How to show / validate error of a node listing page

  17. 17

    java return String[] in method,String[].length 不正确

  18. 18

    使用String.length()和string.toCharArray()。length进行速度/效率权衡

  19. 19

    目标C中string.length和[string length]之间的差异

  20. 20

    How to assign callback return value to a variable in Mongoose?

  21. 21

    How to format Mongoose debug output - pretty print

  22. 22

    mongoose: how to return documents not Cursor with .find()

  23. 23

    无法获取=== string.length才能正常工作

  24. 24

    在Java中,将String :: length用于Comparator.comparing()

  25. 25

    Fixed length, 6-bit binary string in perl

  26. 26

    Order by on length of the string and then by single alphabet desc in pl sql

  27. 27

    MongoDB Length of String or need help in mongoDB mapReduce query

  28. 28

    Index and length must refer to a location within the string error in substring

  29. 29

    String.length如何在JavaScript中工作?

热门标签

归档