【问题标题】:Cannot read property 'splice' of undefined error无法读取未定义错误的属性“拼接”
【发布时间】:2016-07-25 06:19:33
【问题描述】:

我正在尝试使用 php 和 angular 从“客户”表中删除一行。这是我得到的错误

“无法读取未定义的属性‘拼接’”。

有人知道为什么吗?

这是我的代码:

控制者:

$scope.delete = function(deletingId, index) {
        var params = $.param({"customer_id":deletingId});
        $http({
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          url: 'http://localhost:8081/hamatkin/api/delete-customer.php/',
          method: "GET",
          data: params
        }).success(function(data){
         console.log(data);
         $scope.customerDetails = data;
         $scope.customerDetails.splice(index, 1);
        });
      }

客户.php

<?php
class Customer {

  private $table_name = "customers";

  public $customer_id;
  public $kind_Of_Customer;
  public $full_name;
  public $id;
  public $city;
  public $address;
  public $phone;
  public $phone_2;
  public $email;
  public $fax;
  public $referrer;
  public $comments;


}

?>

删除-customer.php

<?php header('Content-Type: text/html; charset=utf-8');
$connect=mysqli_connect("localhost", "root", "", "hamatkin");

  include_once 'Customer.php';

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$customer = new Customer();


if(isset($_GET['customer_id'])){
   $customer_id = $_GET['customer_id'];
   $del = "DELETE FROM posts WHERE customer_id='".$customer_id."'";
   mysql_query($connect, $del);
 }


$newURL = "/hamatkin/#/customerCards";
header('Location: '.$newURL);
?>

html

<tr ng-repeat="x in customers | filter:search_query | orderBy: order_query:reverse_query">
            <td>{{ x.customer_id}}</td>
            <td>{{ x.kind_Of_Customer}}</td>
            <td>{{ x.full_name}}</td>
            <td> {{ x.id}} </td>
            <td> {{ x.city}} </td>
                     <td><a href="/hamatkin/index.html#/customerCardDetails/{{ x.customer_id}}"  class="btn btn-primary btn- active">הצג פרטי לקוח </a></td>
            <td><a ng-click="delete(x.customer_id, $index)" class="btn btn-primary btn- active">מחיקה</td>

当我使用 console.log(data)

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: con in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>5</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>5</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0041</td><td bgcolor='#eeeeec' align='right'>256080</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-query' target='_new'>mysqli_query</a>
(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>5</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: con in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0069</td><td bgcolor='#eeeeec' align='right'>256088</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-query' target='_new'>mysqli_query</a>
(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>6</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: con in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0095</td><td bgcolor='#eeeeec' align='right'>256040</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-query' target='_new'>mysqli_query</a>
(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>7</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\hamatkin\api\delete-customer.php:6) in C:\wamp64\www\hamatkin\api\delete-customer.php on line <i>23</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0115</td><td bgcolor='#eeeeec' align='right'>257184</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.header' target='_new'>header</a>
(  )</td><td title='C:\wamp64\www\hamatkin\api\delete-customer.php' bgcolor='#eeeeec'>...\delete-customer.php<b>:</b>23</td></tr>
</table></font>

【问题讨论】:

    标签: php angularjs


    【解决方案1】:

    这意味着您的$scope.data 未定义。在拼接之前检查$scope.data 是否有一个值并且是一个数组。

    另外,我在您的代码中没有看到$scope.data。你在回调中得到的data 可能就是你需要拼接的那个。

    【讨论】:

    • 谢谢,我更改了代码,我有不同的错误 $scope.customerDetails.splice is not a function
    • 表示 $scope.customerDetails 不是数组。你能在$scope.customerDetails 上做一个console.log 吗?
    • 你的意思是console.log($scope.customerDetails)吗?写在哪里?
    • 是的。在您的应用中打印并将输出发布到您的问题
    • 好的,所以数据是 stringSplice 用于数组,它不适用于字符串
    【解决方案2】:

    $scope.data 未定义,这就是它抛出该错误的原因 只使用数据

    data.splice(index, 1);

    【讨论】:

    • 谢谢,我改成了@Ved 所说的,现在我有错误 - $scope.customerDetails.splice 不是函数
    • 要么你无法获取数据,要么你获取的数据不是数组,所以你最好先记录数据。
    • 你正在获取整个页面而不是你得到的数据,尝试链接到后端而不是 php 页面。
    【解决方案3】:

    $scope.data未定义,因此出现错误。应该是,

    .success(function(data){
    
              data.splice(index, 1);
              $scope.customerDetails = data;
    
            });
    

    或者,

    .success(function(data){
              $scope.customerDetails = data;
              $scope.customerDetails.splice(index, 1);
            });
    

    如果数据不是一个数组,它就不起作用。

    我想应该是,

    .success(function(data){
              console.log(data);
    
              $scope.customerDetails = data.data;
              $scope.customerDetails.splice(index, 1);
            });
    

    【讨论】:

    • 谢谢我改变了你的建议,但现在它说:$scope.customerDetails.splice is not a function
    • 这是因为,$scope.customerDetails 不是一个数组。你能控制 DATA 的帖子并给我回复吗?
    • 我不明白该怎么做?
    • 我的意思是写 console.log(data)。并在浏览器控制台中检查。更新你的qus。用这个值。
    • 在哪里正确呢?在控制台中?对不起,我是新手
    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2020-10-15
    相关资源
    最近更新 更多