Argument Type (typescript)

0

How should I pass the data to the check function?

user.ts

import { Fields } from './fields';
import { dataUser } from './interface';
import { validate } from "class-validator";

export class User  {

    check(data:dataUser){

        let user= new Fields();
        user.name=data.name;
        user.lastname=data.lastname;
        user.document=data.document;
        user.gender=data.gender;
        user.email=data.email;
        user.password=data.password;
        user.terms=data.terms;
        user.privacy=data.privacy;
        user.idDocumentType=data.idDocumentType;
        user.idCity=data.idCity;

        validate(user).then(result => {
            console.log(result);
        });

    }

}

interface.ts

export interface dataUser{
    name: string,
    lastname: string,
    document: string,
    gender: string,
    email: string,
    password: string,
    terms: string,
    privacy: string,
    idDocumentType: number,
    idCity: number,

}

app.ts

import * as $ from 'jquery'
import { User } from './user/user'
import { dataUser } from './user/interface';
const user:User = new User();

$(function() {
    $("#store-user-send").click(function(){

        let dataUsers: dataUser[] =[];
        dataUsers.push({
            name:String( $("#name").val() ),
            lastname:String( $("#lastname").val() ),
            document:String( $("#document").val() ),
            gender:String( $("#gender").val() ),
            email:String( $("#email").val() ),
            password:String( $("#password").val() ),
            terms:String( $("#terms").val() ),
            privacy:String( $("#privacy").val() ),
            idDocumentType:1,
            idCity:1,
        });
        user.check(dataUsers[0]);

    });
});
    
asked by geomerty-matteoquintero 03.05.2018 в 16:17
source

2 answers

1

Looking at the images you can see that the Check method of the User class receives a dataUser object as a parameter, but you in the app.ts file on line 22

user.check(dataUsers);

Being dataUsers an array of the class dataUser so it gives you that error. Observing how you fill the array is not clear to me what you want to do exactly, but if you want to pass the first element you should do

user.check(dataUsers[0]);

This way the code is better for you, since the array does not need it.

    let dataUser: dataUser = {
        name:String( $("#name").val() ),
        lastname:String( $("#lastname").val() ),
        document:String( $("#document").val() ),
        gender:String( $("#gender").val() ),
        email:String( $("#email").val() ),
        password:String( $("#password").val() ),
        terms:String( $("#terms").val() ),
        privacy:String( $("#privacy").val() ),
        idDocumentType:1,
        idCity:1,
    };
user.check(dataUser);
    
answered by 03.05.2018 / 16:37
source
1

The problem must be that you are passing an array of dataUser objects, but the check is receiving only one dataUser object. In the error says that dataUser[] is not assignable to dataUser , an array can not be assigned to an object. In any case you must remove an element from the array (array) and assign it to the function check

    
answered by 03.05.2018 в 16:33