我在这里创建了这个错误的复制品(丑陋地使用了Aurelia但证明了这一点):https://jberggren.github.io/GoogleAureliaBugReproduce/
如果我加载Google API并尝试在Google云端硬盘中列出我的文件,那么从Googles quickstart派生的代码就可以正常工作。如果我在加载Aurelia后使用相同的代码,我会从gapi陈述
中得到一个脚本错误
Uncaught Error: arrayForEach was called with a non array value
at Object._.Sa (cb=gapi.loaded_0:382)
at Object._.eb (cb=gapi.loaded_0:402)
at MF (cb=gapi.loaded_0:723)
at Object.HF (cb=gapi.loaded_0:722)
at Object.list (cb=gapi.loaded_0:40)
at listFiles (index.js:86)
...
在调试时,它似乎是某种阵列检查(Chroms说'本机代码')在Aurelia加载后失败。在我寻找答案的过程中,我发现另外两个人有同样的问题,但没有解决方案(Aurelia gitter question,SO Question)。不知道是否要向Aurelia团队,Google或实际问题所在地报告此事。
帮帮我,你是我唯一的希望。
答案 0 :(得分:2)
这不是一个完美的解决方案,但有效。
https://github.com/aurelia/binding/blob/master/src/array-observation.js
由于某些原因,Aurelia会覆盖Array.prototype.*
。
Gapi lib检查以确保它是否为本机代码。
// example
const r = /\[native code\]/
r.test(Array.prototype.push)
所以,我们必须修补猴子。
gapi.load('client:auth2', async () => {
await gapi.client.init({
clientId: CLIENT_ID,
discoveryDocs: ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
scope: 'https://www.googleapis.com/auth/spreadsheets',
});
// monkey patch
const originTest = RegExp.prototype.test;
RegExp.prototype.test = function test(v) {
if (typeof v === 'function' && v.toString().includes('__array_observer__.addChangeRecord')) {
return true;
}
return originTest.apply(this, arguments);
};
});