无法从 javascript 上的 Backendless table 获取值

Can't get values from Backendless table on javascript

我有 table "Products" 全字段。我尝试在某些条件下查找并获取值。这是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]);

在警报消息中我只收到 "undefined"。而且函数 "get()".

中的 catch 块也没有问题

现在你总是返回一个空数组,即使你的查询找到了一个产品——一个空数组在索引“0”中没有任何内容,因此你得到 'undefined'.

尝试添加

return product;

到 get(),在你查询它之后。

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

console.log(array)

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

alert(JSON.stringify(array));

感谢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();