异步返回已插入post的_id到Mongo collection中
Asynchronously return _id of inserted post into Mongo collection
有数据插入到视频集合,但未来。Return总是只返回空对象。如何返回post _id回到客户端?
// load future from fibers
var Future = Meteor.npmRequire("fibers/future");
// load fibers
var Fiber = Meteor.npmRequire("fibers");
// load youtubedl
var youtubedl = Meteor.npmRequire('youtube-dl');
Meteor.methods({
'command' : function(line) {
// this method call won't return immediately, it will wait for the
// asynchronous code to finish, so we call unblock to allow this client
this.unblock();
var future = new Future();
youtubedl.getInfo(line, function(err, stdout, stderr, videoId) {
if(stdout)
Fiber(function() {
var videoId = Videos.insert({videoObject: stdout ? stdout : stderr});
console.log(videoId);
return videoId;
}).run();
future.return({_id: videoId})
});
return future.wait();
}
});
您正在使用meteorhacks:npm
,它还附带Async工具,更容易使用。https://github.com/meteorhacks/npm async-utilities
,这里有一个例子:
// load future from fibers
var Future = Meteor.npmRequire("fibers/future");
// load fibers
var Fiber = Meteor.npmRequire("fibers");
// load youtubedl
var youtubedl = Meteor.npmRequire('youtube-dl');
Meteor.methods({
'command' : function(line) {
// this method call won't return immediately, it will wait for the
// asynchronous code to finish, so we call unblock to allow this client
this.unblock();
var videoId = Async.runSync(function (done) {
youtubedl.getInfo(url, options, function (err, info) {
if (err) throw new Error(err);
var videoData = {
id: info.id,
title: info.title,
url: info.url //and so on...
};
// var videoId = Videos.insert(videoData);
// for demo purposes we return randomIdHere
var videoId = "randomIdHere"
done(null, videoId); // when done execute this callback with any data
});
});
return videoId;
}
});
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- Javascript复选框函数:;缺少:在属性id之后"
- 在不重构Mongo数据库的情况下,更新嵌入数组中的具有给定ID的对象
- 为什么将 Mongo Id 推送到数组而不是对象
- 我可以通过id从Mongo检索多个文档吗?
- 从id数组到名称数组(mongo,nodejs)
- 通过参数将 mongo 文档 ID 传递给角度指令函数会抛出解析错误
- 不能用动态数组id从流星表单更新mongo值