【发布时间】:2018-05-12 15:29:43
【问题描述】:
我采用了 google nodejs 快速入门 examples 之一作为如何进行 oauth 的模型,将其重写为类对象,以便我可以将其包含在我的更大项目中。我可以成功运行应用程序脚本 api 调用 scripts.run 并在类对象中获得有效的返回值,但不能将其返回到包含的项目。
scripts.run 包含的函数如下所示
Goo.prototype.testAuth = function( auth ){
var script = google.script('v1');
var cmd = {
auth,
resource: { function: 'test_auth', devMode: true },
scriptId: '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
};
script.scripts.run(
cmd,
function( err, resp ){
if( err ){ console.log('The API returned an error: ' + err);return }
if( resp.error ){console.log('Script error message: '
+ resp.error.details[0].error.errorMessage);}
else {
console.log('resp from api call', resp.response.result );
return resp.response.result ;
}
});
};
带有resp.response.result 是有问题的部分,因为它没有传递回容器中的 var response
var Goo = require('./Goo.js');
var goo = new Goo();
var response = goo.test_auth();
console.log('response to use elsewhere ',response);
众所周知,Goo类中的console.log返回一个值,而容器中的console.log返回undefined。
如果重要的话,所有的 Goo 类都这样包装
(function(){
var Goo = (function(){
var Goo = function(config){
}
Goo.prototype.test_auth = function(){
this.authorize( this.testAuth );
};
Goo.prototype.testAuth = function(){};
Goo.prototype.authorize = function(){};
})();
module.exports = Goo;
})();
我应该如何构造它以返回要在容器中使用的值?
我不清楚我是否应该尝试将 script.scripts.run 包装在一个 Promise 中,如果它已经在返回一个 Promise 并且我不知道如何等待它的返回,或者如果我' m 处理回调函数会导致错误的解决方案。感谢您提供此处的任何指导。
我正在使用 node.js 和 googleapis ^24.0.0
【问题讨论】:
-
感谢@danh 做到了
标签: javascript node.js rest google-apps-script promise