gpt4 book ai didi

javascript - 如何从不同设备连接到 firestore 上的同一文档?

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:41 26 4
gpt4 key购买 nike

我有功能:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const Firestore = require('@google-cloud/firestore');
const db = admin.firestore();

exports.createCollectionByPlayerID =
functions.https.onCall((data,context) => {
const citiesRef = db.collection(context.auth.uid);
let setSf = citiesRef.doc('1SF').set({
name: 'San Francisco',
state: 'CA',
country: 'USA',
capital: false,
population: 260
});
return "testID"
});

我希望此函数为每个玩家创建单独的集合(或文档),并以玩家 ID 命名。当我从一台设备调用此函数时,该函数会创建具有一个名称的新集合:

pic 1

当我从另一个设备调用相同的函数时,该函数会创建另一个具有另一个名称的集合。

pic 2

我需要从不同的设备连接到由一名玩家创建的一个 Collections ,而无需创建新的 Collections 。

我的java代码:

private void hw_fromFunction() {
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(@NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String idToken = task.getResult().getToken();

helloWorldFromFunc(idToken).addOnCompleteListener(new
OnCompleteListener<String>() {
@Override public void onComplete(@NonNull Task<String> task) {}
});
} else {}
}
});
}

private Task<String> helloWorldFromFunc(String idToken) {
Map<String, Object> data = new HashMap<>();
data.put("push", true);
return mFunctions
.getHttpsCallable("createCollectionByPlayerID")
.call(idToken)
.continueWith(new Continuation<HttpsCallableResult, String>()
{
@Override
public String then(@NonNull Task<HttpsCallableResult>
task) throws Exception {
return (String) task.getResult().getData();
}
});
}

玩家如何从不同的设备连接到以他的ID命名的一个集合?

===添加了一些java代码:

//>>>>>>>>>>>>>>>>*****auth***********
GoogleSignInOptions gso = new
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
//.requestEmail()
.build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();

mAuth = FirebaseAuth.getInstance();

private void signIn() {Intent signInIntent
= Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);

startActivityForResult(signInIntent, RC_SIGN_IN);
}

private void signOut() {
mGoogleSignInClient.signOut().addOnCompleteListener(this
, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(false);
}
});
}

private void revokeAccess() {
mAuth.signOut();
mGoogleSignInClient.revokeAccess().addOnCompleteListener(this
, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(false);
}
});
}

Firestore 中的规则:

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow create: if request.auth != null && request.auth.uid == uid;
}
}

}

和日志:(抱歉文字太多)

2019-07-05 10:04:21.134 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connecting to remote service
2019-07-05 10:04:21.169 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connection attempt already in progress
2019-07-05 10:04:21.281 8802-8802/com.admiral_odessa.firebaseadmiral1 W/zygote: Skipping duplicate class check due to unrecognized classloader
2019-07-05 10:04:21.283 8802-8802/com.admiral_odessa.firebaseadmiral1 I/DynamiteModule: Considering local module com.google.android.gms.signinbutton_dynamite:0 and remote module com.google.android.gms.signinbutton_dynamite:2
2019-07-05 10:04:21.283 8802-8802/com.admiral_odessa.firebaseadmiral1 I/DynamiteModule: Selected remote version of com.google.android.gms.signinbutton_dynamite, version >= 2
2019-07-05 10:04:21.336 8802-8802/com.admiral_odessa.firebaseadmiral1 W/zygote: Skipping duplicate class check due to unrecognized classloader
2019-07-05 10:04:21.338 8802-8846/com.admiral_odessa.firebaseadmiral1 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-07-05 10:04:21.343 8802-8846/com.admiral_odessa.firebaseadmiral1 I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
2019-07-05 10:04:21.420 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do partial code cache collection, code=30KB, data=28KB
2019-07-05 10:04:21.478 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=28KB, data=28KB
2019-07-05 10:04:21.478 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Increasing code cache capacity to 128KB
2019-07-05 10:04:21.482 8802-8813/com.admiral_odessa.firebaseadmiral1 I/zygote: Background concurrent copying GC freed 11835(1178KB) AllocSpace objects, 1(20KB) LOS objects, 49% free, 1681KB/3MB, paused 781us total 110.398ms
2019-07-05 10:04:21.532 8802-8802/com.admiral_odessa.firebaseadmiral1 D/AutoManageHelper: starting AutoManage for client 0 false null
2019-07-05 10:04:21.545 8802-8802/com.admiral_odessa.firebaseadmiral1 D/AutoManageHelper: onStart true {0=com.google.android.gms.common.api.internal.zaj$zaa@d0b2a33}
2019-07-05 10:04:21.632 8802-8844/com.admiral_odessa.firebaseadmiral1 I/FA: Tag Manager is not found and thus will not be used
2019-07-05 10:04:21.643 8802-8849/com.admiral_odessa.firebaseadmiral1 D/OpenGLRenderer: HWUI GL Pipeline
2019-07-05 10:04:21.692 8802-8851/com.admiral_odessa.firebaseadmiral1 W/DynamiteModule: Local module descriptor class for providerinstaller not found.
2019-07-05 10:04:21.708 8802-8844/com.admiral_odessa.firebaseadmiral1 D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=3100986043983932118}]
2019-07-05 10:04:21.716 8802-8851/com.admiral_odessa.firebaseadmiral1 I/DynamiteModule: Considering local module providerinstaller:0 and remote module providerinstaller:0
2019-07-05 10:04:21.716 8802-8851/com.admiral_odessa.firebaseadmiral1 W/ProviderInstaller: Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
2019-07-05 10:04:21.755 8802-8849/com.admiral_odessa.firebaseadmiral1 I/OpenGLRenderer: Initialized EGL, version 1.4
2019-07-05 10:04:21.755 8802-8849/com.admiral_odessa.firebaseadmiral1 D/OpenGLRenderer: Swap behavior 1
2019-07-05 10:04:21.756 8802-8849/com.admiral_odessa.firebaseadmiral1 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-07-05 10:04:21.756 8802-8849/com.admiral_odessa.firebaseadmiral1 D/OpenGLRenderer: Swap behavior 0
2019-07-05 10:04:21.777 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglCreateContext: 0xa0e9a580: maj 2 min 0 rcv 2
2019-07-05 10:04:21.832 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:21.861 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do partial code cache collection, code=33KB, data=53KB
2019-07-05 10:04:21.886 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=33KB, data=53KB
2019-07-05 10:04:21.886 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Increasing code cache capacity to 256KB
2019-07-05 10:04:21.891 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2019-07-05 10:04:21.891 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2019-07-05 10:04:21.915 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connection attempt already in progress
2019-07-05 10:04:21.916 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connection attempt already in progress
2019-07-05 10:04:21.928 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Activity resumed, time: 32836187
2019-07-05 10:04:22.022 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:22.052 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do full code cache collection, code=125KB, data=66KB
2019-07-05 10:04:22.052 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=118KB, data=47KB
2019-07-05 10:04:22.061 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do partial code cache collection, code=118KB, data=48KB
2019-07-05 10:04:22.061 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=118KB, data=48KB
2019-07-05 10:04:22.061 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Increasing code cache capacity to 512KB
2019-07-05 10:04:22.132 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:22.195 8802-8844/com.admiral_odessa.firebaseadmiral1 D/FA: Connected to remote service
2019-07-05 10:04:22.206 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Processing queued up service tasks: 4
2019-07-05 10:04:22.208 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:22.259 8802-8802/com.admiral_odessa.firebaseadmiral1 W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
2019-07-05 10:04:22.259 8802-8802/com.admiral_odessa.firebaseadmiral1 W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
2019-07-05 10:04:22.265 8802-8802/com.admiral_odessa.firebaseadmiral1 W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
2019-07-05 10:04:22.276 8802-8802/com.admiral_odessa.firebaseadmiral1 I/AssistStructure: Flattened final assist data: 6212 bytes, containing 1 windows, 25 views
2019-07-05 10:04:23.633 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:23.698 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:25.666 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:27.514 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Inactivity, disconnecting from the service
2019-07-05 10:05:29.632 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do full code cache collection, code=249KB, data=151KB
2019-07-05 10:05:29.633 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=158KB, data=105KB

最佳答案

注册用户后,您应该做的第一件事是保存 UID、集合 ID 和文档 ID。任何后续权限或读取都应使用集合 ID 和文档 ID 作为路径,集合\文档 ID。

连接/文档 ID 应通过用户默认值和/或共享存储可供所有设备使用。由于用户默认值保留在每个设备上,因此您需要将用户默认值保存在某种共享存储上。我使用了以下键\值 - UID\集合 ID 和集合 ID/文档 ID。

如果您允许 Firestore 创建文档 ID,它将是唯一的。因此,注册时请保存上述键\值。然后,在后续登录时,首先检查设备的用户默认值中的连接 ID/文档 ID,并将其用作您的路径。如果没有保存,请检查您的共享存储并更新设备的用户默认值。

您还可以使用 Firestore 的命令获取用户的集合 ID。

Swift/iOS

let Database Firestore.firestore()
Database.collection(collection ID).getDocuments{(snapshot, err)}

使用现有帐户登录时,如果您没有找到存储在用户默认值或共享存储中的键/值对,那么您需要根据您的应用程序采取适当的操作。

关于javascript - 如何从不同设备连接到 firestore 上的同一文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com