简体   繁体   English

为什么我的 function 说 promise 处理不正确?

[英]Why is my function saying that a promise isn't being handled correctly?

New to Typescript. Typescript 的新功能。 Wrote this function but I can't figure out why it's giving the error below.写了这个 function 但我不知道为什么它会给出下面的错误。 I commented the 2 lines that the errors are pointing to.我评论了错误指向的两行。

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 error Promises must be handled appropriately or explicitly marked as ignored with the void operator @typescript-eslint/no-floating-promises 48:9 错误 Promise 必须适当处理或使用void运算符 @typescript-eslint/no-floating-promises 明确标记为忽略

61:9 error Promises must be handled appropriately or explicitly marked as ignored with the void operator @typescript-eslint/no-floating-promises 61:9 错误 Promise 必须适当处理或使用void运算符 @typescript-eslint/no-floating-promises 明确标记为忽略

You need to use then with set method to return promise.您需要使用 then 和 set 方法返回 promise。 Like this像这样

    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 => {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM