谷歌翻译API和Firebase Firestore正在互相残杀
我们正在尝试编写一个Google云函数,从Google Translate API获取翻译,然后将结果写入Firebase Firestore数据库。每一个单独起作用,但在一起什么都不起作用。换句话说,我们可以从谷歌翻译获得翻译。我们可以将数据写入Firestore。但如果我们同时尝试这两种方法,我们就无法从Google Translate获得翻译,也无法将任何内容写入Firebase。我们没有收到错误消息。我们已经用谷歌翻译API和Firebase Firestore正在互相残杀,firebase,google-cloud-firestore,google-cloud-functions,google-translate,Firebase,Google Cloud Firestore,Google Cloud Functions,Google Translate,我们正在尝试编写一个Google云函数,从Google Translate API获取翻译,然后将结果写入Firebase Firestore数据库。每一个单独起作用,但在一起什么都不起作用。换句话说,我们可以从谷歌翻译获得翻译。我们可以将数据写入Firestore。但如果我们同时尝试这两种方法,我们就无法从Google Translate获得翻译,也无法将任何内容写入Firebase。我们没有收到错误消息。我们已经用asyncwait和promissions尝试了这段代码。以下是承诺代码:
asyncwait
和promissions尝试了这段代码。以下是承诺代码:
exports.Google_EStranslateEN = functions.firestore.document('Users/{userID}/Spanish/Translation_Request').onUpdate((change, context) => {
if (change.after.data().word != undefined) {
const {Translate} = require('@google-cloud/translate');
const projectId = 'myProject-cd99d';
const translate = new Translate({
projectId: projectId,
});
// The text to translate
const text = change.after.data().word;
// The target language
const target = 'en';
let translationArray = []; // clear translation array
translate.translate(text, target)
.then(results => {
translation = results[0];
translationArray.push(translation);
try {
// write translation to dictionary
admin.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(text).collection('Translations').doc('English').set({
'translationArray': translationArray,
'language': 'en',
'longLanguage': 'English'
})
.then(function() {
console.log("Translation written");
})
.catch(function(error) {
console.error(error);
});
} catch (error) {
console.error(error);
}
})
.catch(error => {
console.error('ERROR:', error);
});
}
});
下面是与异步等待
相同的代码:
exports.Google_EStranslateEN = functions.firestore.document('Users/{userID}/Spanish/Translation_Request').onUpdate((change, context) => { // triggers when browser writes a request word to the database
if (change.after.data().word != undefined) {
async function getTranslation() {
try {
let translationArray = []; // clear translation array
const {Translate} = require('@google-cloud/translate');
const projectId = 'myProject-cd99d';
const translate = new Translate({
projectId: projectId,
});
// The text to translate
const text = change.after.data().word;
const options = {
to: 'en',
from: 'es',
format: 'text'
}
let [translations] = await translate.translate(text, options);
translations = Array.isArray(translations) ? translations : [translations]; // If translations is an array, leave it alone; if not, put it in an array
translationArray.push(translations[0]);
await admin.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(text).collection('Translations').doc('English').set({
'translationArray': translationArray,
'language': 'en',
'longLanguage': 'English'
})
.then(function() {
console.log("Translation written");
})
.catch(function(error) {
console.error(error);
});
// };
} catch (error) {
console.error(error);
}
} // close getTranslation
getTranslation();
}
});
当所有异步工作完成时,您不会返回已解决的承诺。如果您不这样做,云功能将假定您的所有工作都已完成,并将限制所有资源,任何挂起的工作都将被关闭
translate.translate().then().catch()
返回的承诺将被忽略。对admin.firestore()…set()
的嵌套调用也存在类似问题。仅仅对每个承诺调用then()
和catch()
是不够的,因为then()
和catch()
都会返回另一个承诺
您还不必要地将try/catch的用法与promise中的catch()混用在一起。错误处理不需要两种策略,只需要一种或另一种
在第二个示例中使用wait时,您强制JavaScript暂停,直到set()
返回的承诺所表示的异步工作完成。这使得您的函数只有在所有工作完成后才能返回,这就是它工作正常的原因
您可以通过观看我的《关于在云函数中使用承诺和异步/等待》来获得帮助。正确处理承诺对于创建正确工作的函数至关重要。谢谢,我已经用translate.translate()和Firestore get()调用代码库中的其他地方的返回清理了我的代码。我不知道如何返回Firestore set()调用。return admin.Firestore()…set({…})不返回任何内容,并终止translate.translate()调用。我如何返回Firestore set()调用?我读了你的博客文章“为什么Firebase API是异步的?”并尝试了var promise=admin.Firestore()…set()promise.then(),但这不会返回任何内容并终止translate.translate()调用。如果你有多个承诺要等待,请使用promise.all()。你的视频显示的是promise.all()用于并行异步调用,不用于顺序异步调用。我是不是误会了?那么我想我不知道你说的“杀死翻译呼叫”是什么意思。我让代码正常工作,并将其发布为。