我有一个json对象数组,其中包含" USERS"的列表。我尝试使用for循环遍历数组,然后调用另一个函数,无论用户是
//Check for User if already existed or not
queryUserEmail = function(User){
return emailQueryElement.sendKeys(User,protractor.Key.ENTER).then(function(){
return usersListElement.all(by.css('.shortHand')).getText().then(function(text){
if(text.toString().indexOf(User)!=-1){
return true;
}else{
return false
};
});
});
};
this.createUser = function(userArray){
for(var userI=0;userI<userArray.length;++userI){
var UserEmail = userArray[userI].UserEmail;
logger.info('UserEmail outside function::'+UserEmail);
queryUserEmail(UserEmail).then(function(checkFlag){
logger.info('UserEmail inside funtion::'+UserEmail);
});
};
};
和用户数组包含两个用户,当我试图运行函数时,对于函数外的第一次迭代,UserEmail具有数组第一个用户电子邮件,但在函数内部,userEmail有第二个用户的电子邮件。
我在这里错过了什么吗?
答案 0 :(得分:1)
听起来像异步/关闭问题。如果是,请将for ( var userI = 0; userI < userArray.length; ++userI ) {
var UserEmail = userArray[userI].UserEmail;
logger.info('UserEmail outside function::' + UserEmail);
(function (uEmail) {
queryUserEmail(uEmail).then(function(checkFlag){
logger.info('UserEmail inside funtion::' + uEmail);
});
}(UserEmail))
};
函数包装在函数闭包(IIFE)中:
then
当queryUserEmail
(属性for
)触发时,UserEmail
循环已完成,<?php $this->load->view('user/includes/simple_header');?>
<style type="text/css">
.alert { padding: 5px; margin-bottom: 10px; border-radius: 0px; }
.alert-danger { background-color: #D60808; border-color:#D60808; } .btn-primary:hover, .btn-primary:focus,
.btn-primary:active, .btn-primary.active, .open > .dropdown-toggle.btn-primary {border-color:#999999; background-color:#999999 }
.btn-primary { background-color:#d60808; border-color:#d60808; }
.well { padding: 10px; border-radius: 0px; }
</style>
<?php
if(isset($_POST['submit'])) {
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email_address'];
}
// send email
$to = 'test@testing';
$subject = 'Signup Email.';
$msg =
"This is an autogenerated message from PPL Support. Please do not reply to this.\n\n" .
"First Name: $firstname\n" .
"Last Name: $lastname\n" .
"Email: $email\n" .
"Best regards,\n";
$headers = '$headers = 'From: test@test.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();'
mail($to, $subject, $msg, $headers);
?>
<div class="row">
<div class="well col-md-5 center login-box">
<?php
$attributes = array('class' => 'form-horizontal', 'id' => '');
echo validation_errors();
$attributes = array('class' => 'form-signin form-horizontal' , 'id' => 'altForm' , 'name' => 'Signup');
echo form_open_multipart('user/signup', $attributes);
?>
<fieldset class="fieldset_class">
<legend><span class="whitetext">Contact Details</span></legend>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">First Name</span></label>
<div class="col-lg-8">
<?php
$first_name = array(
'name' => 'first_name',
'class' => 'form-control validate[required] text-input',
'placeholder' => 'First name',
'value' => set_value('first_name')
);
echo form_input($first_name);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Last Name</span></label>
<div class="col-lg-8">
<?php
$last_name = array(
'name' => 'last_name',
'class' => 'form-control validate[required] text-input',
'placeholder' => 'Last name',
'value' => set_value('last_name')
);
echo form_input($last_name);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Email Address</span></label>
<div class="col-lg-8">
<span id="msgbox_email" style="display:none"></span>
<?php
$email_address = array(
'name' => 'email_address',
'class' => 'form-control validate[required,custom[email]] text-input',
'placeholder' => 'Email Address',
'id' => 'email_id',
'value' => set_value('email_address')
);
echo form_input($email_address);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Upload Profile Image</span></label>
<div class="col-lg-8">
<?php
$user_image = array(
'name' => 'user_image',
'class' => 'form-control'
);
echo form_upload($user_image);
?>
</div>
</div>
</fieldset>
<fieldset class="fieldset_class">
<legend><span class="whitetext">Your Club Details</span></legend>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">License Type?</span></label>
<div class="col-lg-8">
<?php
$license_type_option = array(
'PPL(A)' => 'PPL(A)',
'CPL(A)' => 'CPL(A)',
'NPPL(A)' => 'NPPL(A)',
'ATPL(A)' => 'ATPL(A)',
'OTHER' => 'OTHER');
echo form_dropdown('license_type', $license_type_option, '', 'class="form-control validate[required]" id="standard-list1"');
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Local Flying Club</span></label>
<div class="col-lg-8">
<?php
$institute = array(
'name' => 'institute',
'class' => 'form-control',
'placeholder' => 'Local Flying Club',
'value' => set_value('institute')
);
echo form_input($institute);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Local Airfield</span></label>
<div class="col-lg-8">
<?php
$local_flying_club = array(
'name' => 'local_flying_club',
'class' => 'form-control',
'placeholder' => 'Local Airfield',
'value' => set_value('local_flying_club')
);
echo form_input($local_flying_club);
?>
</div>
</div>
</fieldset>
<fieldset class="fieldset_class">
<legend><span class="whitetext">Login Details</span></legend>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">User Name</span></label>
<div class="col-lg-8">
<span id="msgbox" style="display:none"></span>
<?php
$username = array(
'name' => 'username',
'class' => 'form-control validate[required] text-input',
'placeholder' => 'User Name',
'id' => 'username',
'value' => set_value('username')
);
echo form_input($username);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Password</span></label>
<div class="col-lg-8">
<?php
$password = array(
'name' => 'password',
'class' => 'form-control validate[required] text-input',
'id' => 'password',
'placeholder' => 'Password'
);
echo form_password($password);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><span class="whitetext">Confirm Password</span></label>
<div class="col-lg-8">
<?php
$password2 = array(
'name' => 'password2',
'class' => 'form-control validate[required,equals[password]] text-input',
'id' => 'password2',
'placeholder' => 'Confirm Password',
);
echo form_password($password2);
?>
</div>
</div>
</fieldset>
<div class="form-group">
<label class="col-lg-4 control-label"></label>
<div class="col-lg-8">
<?php echo form_submit('submit', 'Create Account', 'class="btn btn-primary"'); ?>
</div>
</div>
</div>
</div>
<?php echo form_close();
$this->load->view('user/includes/simple_footer'); ?>
是最后分配的用户。