在Firestore中使用get时如何等待诺言 [英] How to wait for the promise when using get in Firestore

查看:54
本文介绍了在Firestore中使用get时如何等待诺言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在Firestore上尝试一个简单的get命令,使用Google的这段代码是行不通的,因为它没有在等待诺言?

I am just trying a simple get command with Firestore, using this code from Google it doesn't work because it's not waiting for the promise?

我以前有过仅放一小段代码,这就是整个index.js的内容-我正在将Firestore与Dialogflow一起使用,以构建Google Assistant应用,并尝试从欢迎意图中调用函数,该意图从Firestore中获取字段,然后将其写为字段输入一个字符串(名为问题1),然后助手应将此字符串作为ssml响应的一部分。我已经参加了至少30个小时,似乎无法理解关于意图,firestore等方面的承诺。我已经尝试了10种不同的解决方案,这是可行的,只是在其他方面说 undefined我尝试过的变体会多次说未定义,但在2-3次传递后,get命令将完成,然后将读取变量。我只是想弄清楚在进入SSML响应之前如何获取get命令和变量集。有人可以指出我正确的方向吗?

Earlier I had put only a snippet of code, this is the entirety of index.js -- I'm using Firestore with Dialogflow to build a Google Assistant app and trying to call a function from the welcome intent that gets a field from Firestore, then writes that field to a string (named question1), and then this string should be spoken by the assistant as part of the ssml response. I've been on this for at least 30 hours already, can't seem to comprehend promises in regards to intents, firestore, etc. I've tried about 10 different solutions, this one works, only it says "undefined" in other variations I have tried it would say undefined several times but after 2-3 passes the get command would be complete and then the variable would be read out. I'm just trying to figure out how to get the get command and variable set before moving onto the SSML response. Can anyone point me in the right direction?

      'use strict';


const functions = require('firebase-functions'); //don't forget this one

// Import Admin SDK
var admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
const collectionRef = db.collection('text');

const Firestore = require('@google-cloud/firestore');

var doc;

var question1;

const url = require('url');
const {
dialogflow,
Image,
Permission,
NewSurface,
} = require('actions-on-google');
const {ssml} = require('./util');

const config = functions.config();

const WELCOME_INTENT = 'Default Welcome Intent';


const app = dialogflow({debug: true});

async function dbaccess(rando) {


console.log("dbaseaccess started")


var currentquestion2 = 'question-' + rando.toString();

var cityRef
try { return cityRef = db.collection('text').doc(currentquestion2).get();
console.log("get command completed")
//do stuff

question1 = cityRef.data().n111




} catch(e) {

//error!
}
console.log("one line above return something");

return rando;


}


app.fallback((conv) => {
// intent contains the name of the intent
// you defined in the Intents area of Dialogflow
const intent = conv.intent;






switch (intent) {
 case WELCOME_INTENT:

var rando = Math.floor(Math.random() * 3) + 1;
dbaccess(rando);




const ssml =
'<speak>' +
question1 +
'</speak>';
conv.ask(ssml);

break;


exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


推荐答案

您有2个选择:您可以使用 async / await 或根据您希望代码的执行方式使用 Promise.then()

You have 2 options: you can use async/await or you can use Promise.then() depending on how you want the code to execute.

异步/等待:

async function databasetest {
  var cityRef;
  try{
    cityRef = await db.collection('cities').doc('SF');
    // do stuff
  } catch(e) {
    // error!
  }

Promise.then():

Promise.then():

db.collection('cities').doc('SF').then((cityRef) => {
  cityRef.get()
    .then(doc => { /* do stuff */ })
    .catch(err => { /* error! */ });
});

这篇关于在Firestore中使用get时如何等待诺言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆