不返回node.js中的firestore数据库功能 [英] not returning the firestore database function in node.js

查看:42
本文介绍了不返回node.js中的firestore数据库功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是从云功能获取密钥的请求.我在控制台中收到密钥,但没有将我存储值的Firestore数据库返回给我.

Here is a request to get a key from the cloud functions. I am receiving the key in the console but it is not returning me the firestore database where I am storing the values.

这是我的代码:

return request(options, function (error, response, body){
  tokenName = body.notification_key;
  console.log('Key: ' + tokenName);       //gives me the key successfully
  return Promise.all([tokenName]).then(() =>{  //this code doesn't run in the firestore functions console. 
    console.log('key name: ' + tokenName);
    return db.collection('Users').doc(user_id).set({name: name1,token_id: token_id1,notification_key: tokenName,image: image,email: token_email}).then(() => {
      return console.log("Document successfully written!");       
    })
    .catch(function(error) {
      return console.error("Error writing document: ", error);
    });
  });
});

推荐答案

您的代码可以简单得多:

Your code can be much simpler:

return request(options, function (error, response, body){
  tokenName = body.notification_key;
  console.log('Key: ' + tokenName);
  return db.collection('Users').doc(user_id).set({name: name1,token_id: token_id1,notification_key: tokenName,image: image,email: token_email}).then(() => {
    return console.log("Document successfully written!");       
  })
  .catch(function(error) {
    return console.error("Error writing document: ", error);
  });
});

虽然仍然没有将结果写入响应,但是很难确定结果是来自您共享的内容.

That still doesn't write a result to the response though, but it's hard to be certain what the result is meant to be from what you shared.

我不确定您为什么尝试在此处使用 Promise.all().我强烈建议您在继续之前阅读一些有关Promise的教程.此博客文章介绍了基础知识该视频解释了诺言

I'm not sure why you're trying to use Promise.all() here. I highly recommend that you read some tutorials on promises before continuing. This blog post explains the basics, this video explains promises, as does this video.

这篇关于不返回node.js中的firestore数据库功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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