[英]Typescript Object is of Type Unknown, firebase functions
I have a problem with a function for firebase functions, written in typescript.我对用 typescript 编写的 firebase 函数的 function 有问题。
first I have some background info:首先我有一些背景信息:
the data structure of the /tokens ref of the realtime database:实时数据库的 /tokens ref 的数据结构:
Tokens: [
TOKENS_ID: {
token: String
},
]
the following code is giving me compiling errors:以下代码给了我编译错误:
export const onBlogPost = functions.database.ref("/blogs/{bid}")
.onCreate((blogData) => {
console.log(JSON.stringify(blogData.val()));
admin.database().ref("/tokens").once("value")
.then((snapshot) => {
const data = snapshot.val();
const tokens = Object.values(data).map((value) => {
console.log(JSON.stringify(value));
const token = value.token;
return token;
});
return console.log(JSON.stringify(tokens));
});
});
The problem is within the .then((snapshot) => {...}
part. the goal of this part of the code is to transform all the objects within the data snapshot to a single array of just the tokens.问题在于
.then((snapshot) => {...}
部分。这部分代码的目标是将数据快照中的所有对象转换为仅包含标记的单个数组。
the compiling error occurs on the row with the following code:编译错误发生在具有以下代码的行上:
const token = value.token;
the compiling error is as follows:编译错误如下:
src/index.ts(14,29): error TS2571: Object is of type 'unknown'.
What is the correct solution to this, I have tried to assign a type to the variable value in map()
but that was failing horendesly.对此的正确解决方案是什么,我试图为
map()
中的变量值分配一个类型,但这却失败了。
force the type as:强制类型为:
const token: string = value.token;
or或者
const token = value.token as string;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.