我正在编写一个包作为我正在处理的小型应用程序的一部分,我需要做的一件事是从端点获取 json 数据并将其填充到服务器端集合。
我一直收到错误消息,告诉我需要将服务器端集合更新函数放入 Fiber、Meteor.bindEnvironment 或 Meteor._callAsync。
我很困惑,因为没有清晰简明的解释告诉我这些到底是做什么的,它们是什么,是否以及何时被弃用,或者它们的使用是否是好的做法。
下面是我的包文件中重要的内容
api.addFiles([
'_src/collections.js'
], 'server');
一些伪代码:
1) 设置Mongo.Collection项目列表
2) 使用我编写的名为 httpFetch() 的函数填充这些,并为每个集合运行它,如果获取成功则返回已解决的 promise 。
3) 在下划线 each() 循环中调用此 httpFetch 函数,遍历我拥有的每个集合,获取 json 数据,并尝试将其插入服务器端 Mongo DB。
Collections.js 如下所示。将插入函数包装在 Fiber 中似乎可以抑制错误消息,但没有数据被插入到数据库中。
/**
* 服务器端组件向远程请求 * 用于填充服务器端 Mongo 集合的端点。 * * @类服务器 * @静止的 */ 服务器 = {
Fiber: Npm.require('fibers'),
/**
* Collections to be populated with content
*
* @property Collections
* @type {Object}
*/
Collections: {
staticContent: new Mongo.Collection('staticContent'),
pages: new Mongo.Collection('pages'),
projects: new Mongo.Collection('projects'),
categories: new Mongo.Collection('categories'),
formations: new Mongo.Collection('formations')
},
/**
* Server side base url for making HTTP calls
*
* @property baseURL
* @type {String}
*/
baseURL: 'http://localhost:3000',
/**
* Function to update all server side collections
*
* @method updateCollections()
* @return {Object} - a resolved or rejected promise
*/
updateCollections: function() {
var deferred = Q.defer(),
self = this,
url = '',
collectionsUpdated = 0;
_.each(self.Collections, function(collection) {
// collection.remove();
url = self.baseURL + '/content/' + collection._name + '.json';
self.httpFetch(url).then(function(result) {
jsonData = EJSON.parse(result.data);
_.each(jsonData.items, function(item) {
console.log('inserting item with id ', item.id);
self.Fiber(function() {
collection.update({testID: "Some random data"}
});
});
deferred.resolve({
status: 'ok',
message: 'Collection updated from url: ' + url
});
}).fail(function(error) {
return deferred.reject({
status: 'error',
message: 'Could not update collection: ' + collection._name,
data: error
});
});
});
return deferred.promise;
},
/**
* Function to load an endpoint from a given url
*
* @method httpFetch()
* @param {String} url
* @return {Object} - A resolved promise if the data was
* received or a rejected promise.
*/
httpFetch: function(url) {
var deferred = Q.defer();
HTTP.call(
'GET',
url,
function(error, result) {
if(error) {
deferred.reject({
status: 'error',
data: error
});
}
else {
deferred.resolve({
status: 'ok',
data: result.content
});
}
}
);
return deferred.promise;
}
};
我仍然真的被这个问题困住了,从我之前阅读其他帖子的尝试来看,我似乎仍然无法找出使这个工作或让它工作的“最佳实践”方法.
2011/2012 有很多建议,但我不愿意使用它们,因为 Meteor 在不断变化,即使是一个小的更新也会破坏很多东西。
谢谢
最佳答案
好消息:该解决方案实际上比您目前编写的所有代码都简单得多。
据我了解,您编写了一个 httpFetch
函数,该函数使用用 promises 装饰的异步版本的 HTTP.get
。然后,您尝试在新的 Fiber
中运行您的集合更新,因为调用的异步 HTTP.get
引入了一个通过使用 promise then
继续的回调.
您首先需要做的是使用服务器上可用的 HTTP.get
的同步版本,这将允许您编写此类代码:
updateCollections:function(){
// we are inside a Meteor.method so this code is running inside its own Fiber
_.each(self.Collections, function(collection) {
var url=// whatever
// sync HTTP.get : we get the result right away (from a
// code writing perspective)
var result=HTTP.get(url);
// we got our result and we are still in the method Fiber : we can now
// safely call collection.update without the need to worry about Fiber stuff
});
您应该仔细阅读有关 HTTP 模块的文档:http://docs.meteor.com/#http_call
关于javascript - Meteor 0.9.1.1 - 从 json 端点填充到服务器端集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25838362/