【问题标题】:Why is my function saying that a promise isn't being handled correctly?为什么我的函数说没有正确处理承诺?
【发布时间】:2021-05-19 23:16:37
【问题描述】:

Typescript 新手。写了这个函数,但我不知道为什么它会给出下面的错误。我评论了错误指向的两行。

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

exports.createUser = functions.https.onCall(async (data, context) => {
  console.log('_createUser: ');
  const uid = context?.auth?.uid;
  if (uid) {
    const username = data.user;
    const email = data.email;

    //Check to see if that username already exists
    const qData = await admin.firestore().collection('users').where('username', '==', username).limit(1).get();
    qData.forEach(doc => {
      const otherUsername = doc.get('username').toString();

      if (otherUsername) {
        console.log('_createUser: Username is already in use.');
        return 'Username is already in use.'
      }
      else {
        //Create collection for this user's friends list
        const friendsColl = 'friends_' + uid;
        const friendsDoc = admin.firestore().collection(friendsColl).doc();
        friendsDoc.set({ //Error #1 is here
          //Forces the collection to exist
          exists: 1, 

          //Other useful data
          createDate: admin.firestore.FieldValue.serverTimestamp(),
          modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
          ownerUsername: username,
          ownerUID: uid, //
          rowType: 'B', //N = normal, B = backend (created for server side reasons)
        })

        const userDoc = admin.firestore().collection('users').doc(uid); // use uid as document ID
        userDoc.set({ //Error #2 is here
          createDate: admin.firestore.FieldValue.serverTimestamp(),
          modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
          username: username,
          email: email,
          stat: 1, //0 = banned, 1 = normal
          uid: uid,
          friendsColl: friendsColl,
        })
        return console.log('_createUser_finished');
      };
    });    
  }
  else {
    return console.log('_createUser_Error: User is not authorized');
  };
});

48:9 错误 Promises 必须得到适当处理或使用 void 运算符 @typescript-eslint/no-floating-promises 明确标记为忽略

61:9 错误 Promise 必须得到适当处理或使用 void 运算符 @typescript-eslint/no-floating-promises 明确标记为忽略

【问题讨论】:

    标签: javascript typescript firebase google-cloud-functions


    【解决方案1】:

    你需要使用 then 和 set 方法来返回 promise。像这样

        friendsDoc.set({ //Error #1 is here
          //Forces the collection to exist
          exists: 1, 
    
          //Other useful data
          createDate: admin.firestore.FieldValue.serverTimestamp(),
          modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
          ownerUsername: username,
          ownerUID: uid, //
          rowType: 'B', //N = normal, B = backend (created for server side reasons)
        }).then(result => {});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 2019-08-21
      • 1970-01-01
      • 2018-07-09
      • 2021-01-31
      • 2021-01-02
      • 2021-04-01
      相关资源
      最近更新 更多