gpt4 book ai didi

mongodb - 如何查询Breeze.js knockout.js mongoHQ curl

转载 作者:可可西里 更新时间:2023-11-01 09:52:35 25 4
gpt4 key购买 nike

如何使用 Breeze 从 MongoHQ 获取数据?到目前为止,我已经试过了:

this.getDataFormServer = function (formElement) {

$.ajax({
url: "https://api.mongohq.com/databases/mydataBase/collections/customers/documents?_apikey=aabbddkkddiieeoollddd33kk3",
type: "GET",
data: {},
datatype: "json",
processData: false,
contentType: "application/json; charset=utf-8",
success: function (resultSuccess) {
//alert("Success: " + JSON.stringify(resultSuccess));
console.log(resultSuccess);
},
complete: function (response) {
//alert('your datas are now saved');
},
error: function (xhr, status, error) {
console.log(status);
}
});
}

它运行良好,但现在我想对 Breeze 做同样的事情,这就是我所拥有的:

<!-- Knockout template -->
<ul data-bind="foreach: results">
<li>
<span data-bind="text:FirstName"></span>
<span data-bind="text:LastName"></span>
</li>
</ul>

从查询绑定(bind)到员工:

manager.executeQuery(breeze.EntityQuery.from("Customers"))
.then(function(data){
ko.applyBindings(data);
});

谁能帮我解决这个问题?谢谢。

更新:我在这里做错了什么

 this.getDataFormServer = function (formElement) {
var EntityQuery = breeze.EntityQuery;
var manager = new breeze.EntityManager('https://api.mongohq.com/databases/myMongoHq/collections/customers/documents?_apikey=aelctgd3p3czwh6zx5uy&limit=4');

var getRemoteDocuments = function () {
var query = EntityQuery.from('documents');
return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
};
function querySucceeded(data) { console.log('Retrieved documents from remote data source'); }
function queryFailed(data) { console.log('Failed to retrieve documents from remote data source'); }

var getLocaldocuments = function () {
console.log("getLocals called");

var newQuery = new EntityQuery('documents');
var Documents = manager.executeQueryLocally(newQuery);
if (Documents) console.log("retrieved some cars from local cache");
else console.log("no cars retrieved from local cache");
};

getRemoteDocuments().then(getLocaldocuments);

}

在控制台我只能看到这个:

Failed to retrieve documents from remote data source anfrageerstellen.js:222
getLocals called

最佳答案

我还没有真正看到你的问题是什么,所以我只是想尝试一下。查看我将任何 API 与 Breeze 结合使用制作的示例片段,这可能有助于找到如何连接到您的 API,在这种情况下,它似乎是托管 Mongo DB 的服务器。在不知道您遇到的任何问题的情况下,很难给出更具体的建议 -

这是一个数据上下文示例,您可以将其用作路线图 -

function () {
var EntityQuery = breeze.EntityQuery;

var serviceName = 'https://api.mongohq.com/databases/mydataBase/collections/customers/'

var myAPIKEY = "yourkeygoeshere";

var ds = new breeze.DataService({
serviceName: serviceName,
// You will need to set your models up server side in a models class of some sort
hasServerMetadata: false
});

function configureBreezeManager() {
var mgr = new breeze.EntityManager({ dataService: ds });
return mgr;
}

var manager = configureBreezeManager();

// Create your models in a model file or something
model.initialize(manager.metadataStore);

var metadataStore = manager.metadataStore;

var getDocuments = function () {
var parameters = makeParameters();
var query = breeze.EntityQuery
.from("documents")
.withParameters(parameters)
.toType('Documents');

return manager.executeQuery(query).then(querySucceeded).fail(queryFailed);

function querySucceeded(data) {
return data.results;
}
};

function makeParameters(addlParameters) {
var parameters = {
apikey: myAPIKEY
};
return breeze.core.extend(parameters, addlParameters);
}

function queryFailed(error) {
console.log('Error retrieving data. ' + error.message);
}
});

不知道这是否是您正在寻找的,您可能还需要做更多工作,例如设置您的模型,如果您要从服务器返回复杂的结构,您可能需要将它们映射回您的对象等......但至少这应该让您从服务器返回相同的数据。如果没有,并且您遇到任何错误,请告诉我原因,我会尽力提供帮助。

祝你好运。

关于mongodb - 如何查询Breeze.js knockout.js mongoHQ curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19159719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com