【发布时间】:2019-10-07 05:19:05
【问题描述】:
我第一次使用 firebase 作为我的服务器和数据库,我正在尝试将我的 firebase 后端功能部署到 firebase 我在控制台中不断收到关于未嵌套我的承诺的错误:
"error 每个 then() 都应该返回一个值或者抛出 promise/always-return
52:16 警告避免嵌套承诺承诺/禁止嵌套
52:16 警告避免嵌套承诺 promise/no-nesting"
还有其他写这个承诺的方法吗?
let Promise = require('promise');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.addSimilarImages =
functions.firestore.document('photos/{document}')
.onCreate((snap, context) => {
console.log('SNAP', snap)
console.log('CONTEXT', context)
by recreating a google storage style url called photoUrl
const data = snap.data();
console.log('DATA IN IS', data)
const photoUrl = "gs://" + data.bucket + "/" + data.fullPath;
conolse.log('url is', photoUrl);
return Promise.resolve()
.then(() => {
//we put the photoUrl through the vison API and it returns a list of similar images
return visionClient.webDetection(photoUrl);
}) //place these similar images in a array
.then(results => {
console.log('VISION data all is: ', results)
const webDetection = results[0].webDetection
//update the document in the photos collection with the similarImages images
let similarImages = [];
if (webDetection.visuallySimilarImages.length) {
webDetection.visuallySimilarImages.forEach(image => {
similarImages.push(image);
});
}
console.log('similarImages', similarImages)
db.collection('photos').doc(context.params.document).update({
similarImages })
})
.catch(err => console.log(err));
})
.then(res => console.log('pictures added'))
.catch(err => console.log(err));
【问题讨论】:
标签: javascript firebase promise