Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript/NodeJs-TypeError:无法设置属性';验证';未定义的_Javascript_Node.js - Fatal编程技术网

JavaScript/NodeJs-TypeError:无法设置属性';验证';未定义的

JavaScript/NodeJs-TypeError:无法设置属性';验证';未定义的,javascript,node.js,Javascript,Node.js,我想验证用户表单输入,我只是写了这段代码,但实际上有一个错误我无法修复,我不明白为什么会出现(错误) 还有我犯的错误 User.prototype.validation = () => { ^ TypeError: Cannot set property 'validation' of undefined at Object.<anonymous> (C:\Users\40sherrin\Desktop\applic

我想验证用户表单输入,我只是写了这段代码,但实际上有一个错误我无法修复,我不明白为什么会出现(错误)

还有我犯的错误

User.prototype.validation = () => {
                          ^

TypeError: Cannot set property 'validation' of undefined
    at Object.<anonymous> (C:\Users\40sherrin\Desktop\application\models\User.js:5:27)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\40sherrin\Desktop\application\controllers\userController.js:1:76)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
User.prototype.validation=()=>{
^
TypeError:无法设置未定义的属性“validation”
at对象。(C:\Users\40sherrin\Desktop\application\models\User.js:5:27)
at模块编译(内部/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:700:10)
在Module.load(内部/modules/cjs/loader.js:599:32)
在tryModuleLoad(内部/modules/cjs/loader.js:538:12)
at Function.Module._load(内部/modules/cjs/loader.js:530:3)
at Module.require(内部/modules/cjs/loader.js:637:17)
根据需要(内部/modules/cjs/helpers.js:22:18)
at对象。(C:\Users\40sherrin\Desktop\application\controllers\userController.js:1:76)
at模块编译(内部/modules/cjs/loader.js:689:30)

您不能将箭头函数用于

arrow函数表达式是一种语法紧凑的替代方法 一个正则函数表达式,虽然没有它自己的绑定到 this、arguments、super或new.target关键字。箭头函数 表达式不适合用作方法,并且不能用作 构造函数

原型属性的使用

箭头函数没有原型属性

因此,使用
函数
代替ES6类


类用户{
建造师(数据){
this.data=数据
这是错误=[]
}
验证(){
// ...
}
寄存器(){
这是验证();
}
} 

您不能将箭头函数用于

arrow函数表达式是一种语法紧凑的替代方法 一个正则函数表达式,虽然没有它自己的绑定到 this、arguments、super或new.target关键字。箭头函数 表达式不适合用作方法,并且不能用作 构造函数

原型属性的使用

箭头函数没有原型属性

因此,使用
函数
代替ES6类


类用户{
建造师(数据){
this.data=数据
这是错误=[]
}
验证(){
// ...
}
寄存器(){
这是验证();
}
} 

箭头函数的存在是为了解决一个特定的问题:访问包含的词法
this
。当使用
新的
操作符调用时,构造函数依赖于接收
this
,作为从函数原型继承的对象。这就是为什么箭头函数不能用作构造函数的原因d如果将它们用作
prototype
方法,它们将无法访问对象实例。箭头函数的存在是为了解决一个特定问题:访问封闭的词法
this
。当使用
new
操作符调用时,构造函数依赖于将
this
作为从函数继承的对象来接收这就是为什么箭头函数不能用作构造函数,如果你将它们用作
prototype
方法,它们将无法访问对象实例。
User.prototype.validation = () => {
                          ^

TypeError: Cannot set property 'validation' of undefined
    at Object.<anonymous> (C:\Users\40sherrin\Desktop\application\models\User.js:5:27)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\40sherrin\Desktop\application\controllers\userController.js:1:76)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
let User = function (data) {
    this.data = data
    this.errors = []
}
User.prototype.validation = function() {
    if(this.data.username == ""){
        this.errors.push("You must* provide a username")
    }else if(this.data.email == ""){
        this.errors.push("You must* provide the email address for you account")
    }else if(this.data.password == ""){
        this.errors.push("You must* provide the password for your account")
    }
}
User.prototype.register = function() {
    // step #1 validate user data
    this.validation()
    // step #2 only if there are no validation errors
    // then save the user data into a database
}