我来自移动应用程序开发,对打字稿没有太多经验。如何声明 [string:any] 形式的地图对象?
错误出现在行:map[key] = value;
元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“Object”类型。
在类型“Object”.ts(7053) 上找不到带有“string”类型参数的索引签名
var docRef = db.collection("accidentDetails").doc(documentId);
docRef.get().then(function(doc: any) {
if (doc.exists) {
console.log("Document data:", doc.data());
var map = new Object();
for (let [key, value] of Object.entries(doc.data())) {
map[key] = value;
// console.log(`${key}: ${value}`);
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
} }).catch(function(error: any) {
console.log("Error getting document:", error);
});
Run Code Online (Sandbox Code Playgroud) 每当在集合“用户”中添加新文档时,我都会尝试创建一个新的经过身份验证的用户。用户详细信息应在新文档的“电子邮件”和“电话”字段中提供。
下面的代码仅在创建新文档时触发,但无法获取该文档中的值来创建具有电子邮件、电话和随机 userId 的经过身份验证的用户。
import * as functions from 'firebase-functions';
const admin = require('firebase-admin');
admin.initializeApp();
exports.updateUser = functions.firestore
.document('users/{user}').onCreate((snap, context) => {
// ... Your code here
const newValue = snap.data();
const email = newValue.email;
const phone = newValue.phone;
console.log(newValue);
console.log("new user added");
admin.auth().createUser({
uid: 'some-uid',
email: email,
phoneNumber: phone
})
.then(function() {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully created new user:');
})
.catch(function() {
console.log('Error creating new user:');
});
});
Run Code Online (Sandbox Code Playgroud)
对于这些代码行中的 const“newValue”,我收到错误“对象可能未定义”。
const …
Run Code Online (Sandbox Code Playgroud) node.js firebase typescript google-cloud-functions google-cloud-firestore