简体   繁体   English

如何修复“ReferenceError:index is not defined”问题

[英]How to fix “ReferenceError: index is not defined” problem

I try to connect my REST server with JavaScript app. 我尝试将我的REST服务器与JavaScript应用程序连接。 Using ajax query I get proper JSON, but I cannot bind it to my HTML website. 使用ajax查询我得到了正确的JSON,但我无法将其绑定到我的HTML网站。 I use data-bind in HTML: 我在HTML中使用data-bind:

<tbody>
  <tr >
    <td> <input type="number" data-bind="value: index" name="index" readonly>  </td>
    <td> <input type="text" data-bind="value: name" name="name"required> </td>
    <td> <input type="text" data-bind="value: surname" name="surname"required> </td>
    <td> <input type="date" data-bind="value: birthdate" name="birthdate" min="1950-01-01" max="2050-01-01" required> </td>
    <td><button type="button" >Edit</button></td>
  </tr>
</tbody>

In .js file I have below code: 在.js文件中我有以下代码:

'use strict';

var URL = 'http://localhost:8000/'

$(document).ready(function(){

var StateViewModel = function () {
    var self = this;
    self.students = ko.observableArray();

    $.ajax({
        url: URL + 'students',
        type: 'GET',
        dataType: 'json',
        accepts: {
            contentType: 'application/json'
        }
    }).done(function(result) {
        console.log(result)
        ko.mapping.fromJS(result, self.students);
    });
}

var model = new StateViewModel();
ko.applyBindings(model);

});

And I get "ReferenceError: index is not defined" error. 我得到“ReferenceError:索引未定义”错误。

When I request my REST appI get below JSON: 当我请求我的REST appI低于JSON时:

[
{
    "index": 127001,
    "name": "John",
    "surname": "Smith",
    "birthdate": "1996-11-11"
},
{
    "index": 127002,
    "name": "Abcd",
    "surname": "Xyz",
    "birthdate": "1996-11-01"

}
   ]

And in ajax function .done result is: 并在ajax函数.done结果是:

0: Object { index: 127001, name: "John", surname: "Smith", … }
1: Object { index: 127002, name: "Abcd", surname: "Xyz", … }

What could be the reason of "ReferenceError: index is not defined" error? 可能是“ReferenceError:index is not defined”错误的原因是什么?

Taken straight from the docs 直接从文档中获取

The first time you fetch data you should do this 第一次获取数据时,您应该这样做

var viewModel = ko.mapping.fromJS(data);

Then every time data is received from the server after that 然后每次从服务器收到数据

ko.mapping.fromJS(data, viewModel);

Your JSON looks fine, and there's nothing wrong with using 3 arguments for your mapping.fromJS with an empty object as the "options" argument. 你的JSON看起来很好,使用3个参数进行map.fromJS并将空对象作为“options”参数没有任何问题。 My guess is that it's a context issue with which object your markup is attempting to bind to. 我的猜测是,这是一个上下文问题,你的标记试图绑定到哪个对象。 If you're still at the root level view-model the binding will fail because "Index" doesn't exist at the root level. 如果您仍处于根级别视图模型,则绑定将失败,因为根级别不存在“索引”。 You need a foreach binding to nest into the "students" child object. 你需要一个foreach绑定嵌套到“students”子对象中。

 var URL = 'http://localhost:8000/'; var sampleData = [{ "index": 127001, "name": "John", "surname": "Smith", "birthdate": "1996-11-11" }, { "index": 127002, "name": "Abcd", "surname": "Xyz", "birthdate": "1996-11-01" } ]; var StateViewModel = function() { var self = this; self.students = ko.observableArray(); setTimeout(function() { //console.log(sampleData) ko.mapping.fromJS(sampleData, {}, self.students); }, 1000); } var model = new StateViewModel(); ko.applyBindings(model); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script> <table> <tbody data-bind="foreach: students"> <tr> <td> <input type="number" data-bind="value: index" name="index" readonly> </td> <td> <input type="text" data-bind="value: name" name="name" required> </td> <td> <input type="text" data-bind="value: surname" name="surname" required> </td> <td> <input type="date" data-bind="value: birthdate" name="birthdate" min="1950-01-01" max="2050-01-01" required> </td> <td><button type="button">Edit</button></td> </tr> </tbody> </table> 

I also managed to solve my problem in this way: 我也设法通过这种方式解决了我的问题:

'use strict';

var URL = 'http://localhost:8000/'

$(document).ready(function(){
    console.log("Abc")
    ko.applyBindings(new customerViewModel());
});

function customerViewModel() {
    var self = this;
    self.studentsList = ko.observableArray();

    $.ajax({
        type: 'GET',
        url: URL + 'students',
        contentType: "application/json",
        dataType: "json",
    success: function(data) {
        console.log(data)
        var observableData = ko.mapping.fromJS(data);
        var array = observableData();
        self.studentsList(array);
     },
    error:function(jq, st, error){
        alert(error);
    }
});
}

And using foreach 并使用foreach

data-bind="foreach: studentsList"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM