Help with concatenating

Asked

Viewed 87 times

1

I need to pass a variable inside an item array, a parameter that only accepts string, but when I pass a variable the API does not accept, it says that the parameter is incorrect.

Better explaining:

 var userstocreate = [
 {
     username: 'aqui a variavel',
     password: 'senha',
     firstname: 'nome',
     lastname: 'sobrenome',
     email: 'email'
 }

In the username where is 'here the variable' I have to pass between simple quotes.

Someone has a light to do it?

Got it, thanks for all your help.

   var userstocreate = [
   {
    username: '' + usuario + '',
    password: 'E@d123456',
    firstname: '' + primeiro +'',
    lastname: '' + lastname + '',
    email: '' + email + ''
   }
 ];

var primeiro = this.firstname.toString();
var usuario = this.username.toString();
var lastname = this.lastname.toString();
var email = this.email.toString();
  • The variable that Voce is trying to pass is already a string?

  • Yes var user = this.username; is a string, I have tried to convert I have already created a variable for example var name = "starname"; ai when I try to put in usernaem : name. does not work at all.

  • You’ve tried to get past her without any kind of conversation?

  • Already tried Raphael, this API is from MOODLE I need to make a student registration coming from the bank of the institution, it’s all ready, already listed the students, already has the service, that brings everything in json, ta all ready, just need to put the variable inside the array of users.

  • have done that. var user = "user agoravai" var userstocreate = [ , username: user, password: 'E@d 123456', firstname: 'testfirstname1', lastname: 'testlastname1', email: 'stage [email protected]' } ];

  • tries this concatenation, username: '"'+ username + '"',

  • @Yurirodrigues Poste as answer instead of editing the content of the question.

Show 2 more comments

2 answers

0

Try using escape characters like \' to first get the value of the variable and make it a string

username: "\'"+ variavel +"\'",
  • did not :( responseText: "{"Exception":"invalid_parameter_exception","errorcode":"invalidparameter","message"

  • then your problem is that the API would only accept that you pass up the value 'name' and not "name", very strange this, by default javascript encapsula string with double quotes, I do not know if I could convert to simple

  • I can pass "name", the accepted api only does not accept a variable.

  • @Yurirodrigues using that answer, it wouldn’t just be ''+variavel+''?

  • Already tried var userstocreate = [ { username: '+user+', password: 'E@d 123456', firstname: 'Julia', lastname: 'testefinal', email: '[email protected]' } ]; and will not.

  • Guys worked! Thank you so much everyone! username: '' + user + ', order so. Thank you so much!

Show 1 more comment

0

I honestly don’t understand. You want the username to have a value of a variable?

If that’s what you have to do.

var akira = "texto"

example;

var userstocreate = [
    {
        username: akira,
        password: 'senha',
        firstname: 'nome',
        lastname: 'sobrenome',
        email: 'email'
    }
]

or

var userstocreate = [
    {
        username: '', //opcional definir
        password: 'senha',
        firstname: 'nome',
        lastname: 'sobrenome',
        email: 'email'
    }
]
userstocreate[0].username = akira
  • Where is username I need to pass a variable, only this, however I need to pass it as string, I tried to convert the variable, I tried everything but it does not work, if I do not pass inside the simple quotes the API does not accept, understood?

  • Akira, use the fields of the comments to ask about the real need of those who ask, it facilitates both in the elaboration of an accurate answer, and avoids content like "Honestly I did not understand. Do you want the username to have a value of a variable?" in the body of the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.