【发布时间】:2019-07-14 01:47:16
【问题描述】:
这是我的代码 sn-p:
await firestore.runTransaction(t => {
t.get(docRef)
.then(docRef => {
logger.info("entering transaction");
if (!docRef.exists) {
t.create(docRef, new Data);
return Promise.resolve();
} else {
t.update(docRef, {aField: updateData});
return Promise.resolve();
}
})
.catch(error => {
return Promise.reject(error);
});
})
这给了我以下错误:
Error: You must return a Promise in your transaction()-callback. at transaction.begin.then (/srv/node_modules/@google-cloud/firestore/build/src/index.js:496:32) at <anonymous>
这是我第一次使用 firestore 事务语法,我对 nodejs promise 的概念和语法相当了解。我模仿了firestore事务示例here,并编写了上面的sn-p。
报错后,我尝试将Promise.resolve()改为Promise.resolve(0),但没有成功。
我也尝试删除 await 并改用 .then,但它仍然给我同样的错误。
请给它一些亮点。欣赏它。谢谢。
【问题讨论】:
-
我知道这不能回答你的问题,但如果其他人在这里是因为他们使用的是 Bluebird 承诺而不是原生承诺,firebase 代码会检查
promise instanceof Promise并且 Bluebird 承诺将失败那张支票。我不得不用一个原生的承诺来包装我的。
标签: node.js promise google-cloud-firestore es6-promise