【问题标题】:Can't get values from Backendless table on javascript无法从 javascript 上的 Backendless 表中获取值
【发布时间】:2016-03-11 06:16:58
【问题描述】:

我有包含完整字段的“产品”表。我尝试在某些条件下查找并获取值。这是javascript文件

var APPLICATION_ID = "id",
SECRET_KEY = "key",
VERSION = "v1"; 

Backendless.initApp(APPLICATION_ID, SECRET_KEY, VERSION);

function Products(args) {
    args = args || {};
    this.id_user = args.id_user || "";
    this.cal = args.cal || null;
    this.id_day = args.id_day || null;
    this.id_product = args.id_product || null;
    this.nameProduct = args.nameProduct || "";
}

var productStorage = Backendless.Persistence.of( Products );

var query = { 
    condition : "id_day = 33"
}

function get(){
    try{
        var product = productStorage.find(query).data;
    }catch( e )
    {
        if( e.code != 1009 )
        alert(e.message);
    }

    return [];
}


var array = get();
alert(array[0]);

在警报消息中,我得到的只是“未定义”。并且函数“get()”中的catch块也没有问题。

【问题讨论】:

    标签: javascript backendless


    【解决方案1】:

    现在您总是返回一个空数组,即使您的查询找到了一个产品 - 一个空数组在索引“0”中没有任何内容,因此您会得到“未定义”。

    尝试添加

    return product;
    

    在您查询它之后立即获取()。

    使用console.log 而不是alert 调试会更容易 - 您需要打开浏览器的 JavaScript 控制台才能看到它:

    console.log(array)
    

    如果您确实选择使用alert,将值转换为 JSON 会有所帮助:

    alert(JSON.stringify(array));
    

    【讨论】:

    • 看来你的答案是正确的,但我还是得到了“未定义”.. 似乎我的查询有误或使用 Backendless..
    • 所以,我尝试更改代码product= productStorage.find(query); 并写了var array = get(); console.log(array);,我得到了“response:”.... class":"Products","id_user":"333","ownerId控制台中的 XMLHttpRequest{} 中的“:null......”。这是我需要得到的。所以我的信息是存在的,但是如何从响应中获取呢?
    • 完整回复是response: "{"offset":0,"data":[{"id_day":"33","id_product":null,"created":145....00,"___class":"Products","id_user":"333","ownerId":null,"updated":14....000,"nameProduct":"iris","objectId":"DE8....4F00","cal":null,"__meta":"{\"relationRemovalIds\":{},\"selectedProperties\":[\"id_day\",\"id_product\",\"created\",\"___class\",\"id_user\",\"ownerId\",\"updated\",\"nameProduct\",\"objectId\",\"cal\"],\"relatedObjects\":{}}"}],"nextPage":null,"totalObjects":1}"
    【解决方案2】:

    感谢 Backendless 团队,他们帮助我解决了这个问题。所以我的代码:

    var APP_ID = "id",
    SECRET_KEY = "key",
    VERSION = "v1"; 
    
    function Products(args) {
        args = args || {};
        this.id_user = args.id_user || "";
        this.cal = args.cal || null;
        this.id_day = args.id_day || null;
        this.id_product = args.id_product || null;
        this.nameProduct = args.nameProduct || "";
    }
    
    var dataQuery = {
       condition: "id_day = 33"
    };
    
    function fetchingFirstPageAsync(){
    
        var products = Backendless.Persistence.of(Products).find(dataQuery);
    
        for(var i = 0; i < products.data.length; i++) {
            console.log("Product name = " + products.data[i].nameProduct);
        }
    }
    
    Backendless.initApp( APP_ID, SECRET_KEY, VERSION );
    fetchingFirstPageAsync();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多