【问题标题】:Knockout JS Uncaught TypeError: Cannot read property 'fromJS' in Oracle CloudKnockout JS Uncaught TypeError:无法在 Oracle Cloud 中读取属性“fromJS”
【发布时间】:2018-01-08 10:03:01
【问题描述】:

我在 Knockout.js 中收到此错误:

未捕获的类型错误:无法读取未定义的属性“fromJS”

我是 Knockout JS 的新手。我正在 Oracle Content and Experience Cloud 中开发 Knockout JS。我的场景是我正在尝试调用 Rest API 并获取结果并将其显示在表格中。

我正在使用下面的 REST API url 在我的应用程序中进行测试 http://learn.knockoutjs.com/mail?folder=inbox

下面是我的代码:

/* globals define */
define(['knockout', 'jquery', 'text!./knockout.mapping-latest.js', 'text!./mailbox.json', 'css!./css/design.css'], function (ko, $, mapping, css) {
    'use strict';
    // ----------------------------------------------
    // Define a Knockout Template for your component
    // ----------------------------------------------
    var sampleComponentTemplate = 
        '<div>' + 
        '<p><input data-bind="value: searchValue" class="box"/></p>' +
        '<button data-bind="click: getCustomers">Knock out Search</button>' +
        '</div>' +  
        '<table>' +
            '<thead>' +
                '<tr>' +                
                    '<th>From</th>' + 
                    '<th>To</th>' + 
                    '<th>Subject</th>' + 
                    '<th>Date</th>' + 
                '</tr>' + 
            '</thead>' + 
            '<tbody data-bind="foreach: mails">' + 
                '<tr>' +
                    '<td data-bind="text: from"></td>' +  
                    '<td data-bind="text: to"></td>' + 
                    '<td data-bind="text: subject"></td>' + 
                    '<td data-bind="text: date"></td>' +                    
                '</tr>' +
            '</tbody>' + 
        '</table>';



// ----------------------------------------------
    // Define a Knockout ViewModel for your template
    // ----------------------------------------------
    var SampleComponentViewModel = function (args) {
    this.searchValue = ko.observable("Hi");

 this.mails = ko.observableArray();    
  this.getCustomers = function () {     
    alert("Inside get customers ");
        $.ajax({
            type: 'GET',             
            crossDomain: true, 
            url: 'documents/folder/F49A137E34CB4B6DFD302FB90A04F4D8CA1E8A3D5B3E/_assets/mailbox.json',
            data: JSON.stringify(this.mails),             
            success: function(data) {           
                var observableData = ko.mapping.fromJS(data);
                var array = observableData();
                this.mails(array);

            },
            error:function(jq, st, error){
                alert("Inside Error Method " + error + " jq is " + jq + "st is " + st);
            }
        });
    };

    };

【问题讨论】:

  • 请注意Please correct my code as well if there are any mistakes 是免费劳动力的请求。读者愿意帮助您,但不会为您工作。我们每天都有数百人对志愿者寄予厚望,最好确保您的问题不像他们的问题:-)
  • 所以,我的第一反应是在您的 Ajax 成功处理程序的第一行中不存在 ko.mapping 属性。您是否检查过以确保您正确加载了所有 KO 依赖项?看到这个问题:stackoverflow.com/questions/14441276/…
  • 嗨,Paul,我在初始化本身中加载了 knockout.mapping-latest.js 文件。但不知何故 fromJS 无法识别。我什至尝试了 mapping.fromJS 也没有工作
  • @Rajesh - 尝试将ko.mapping.fromJS(data) 更改为mapping.fromJS(data)。您可能需要进行更改,因为您似乎将映射 Knockout 插件附加到 mapping
  • @Paul 你是对的,即使我尝试使用 mapping.fromJS(data),我也在将淘汰插件映射到映射。我仍然收到错误,如下 Uncaught TypeError: mapping.fromJS is not a function

标签: javascript knockout.js oracle-cloud-infrastructure-classic


【解决方案1】:

我通过初始化 self =this 解决了这个问题,并将代码中的 this 替换为 self

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 2021-11-02
    • 2018-10-05
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多