为什么我得到这个解构赋值代码 'undefined is not a function'?
Why am I getting 'undefined is not a function' for this destructuring assignment code?
假设我有这个请求进入 API 路线:
/api/things?p=2&t=25&some_id=67&another_id=89
我可以使用destructuring assignment将参数获取到局部变量吗?我试过这个:
const [p, t, ...otherParams] = request.query;
...但我在运行时得到 "undefined is not a function",这是一个我无法调试的奇怪错误。我真正喜欢的是使用带有默认值的解构。在我有缺陷的语法中,这将是这样的:
const [p = 1, t = 25, ...otherParams] = request.query;
这可能吗?如果是这样,我做错了什么?
您正在解构的值是一个对象,而不是数组。因此,您需要使用对象解构语法。如果你使用了错误的口味,你会得到不太有用的 "undefined is not a function" 错误。
const {p = 1, t = 25, ...otherParams} = request.query
上周五我遇到了同样的错误。
假设我有这个请求进入 API 路线:
/api/things?p=2&t=25&some_id=67&another_id=89
我可以使用destructuring assignment将参数获取到局部变量吗?我试过这个:
const [p, t, ...otherParams] = request.query;
...但我在运行时得到 "undefined is not a function",这是一个我无法调试的奇怪错误。我真正喜欢的是使用带有默认值的解构。在我有缺陷的语法中,这将是这样的:
const [p = 1, t = 25, ...otherParams] = request.query;
这可能吗?如果是这样,我做错了什么?
您正在解构的值是一个对象,而不是数组。因此,您需要使用对象解构语法。如果你使用了错误的口味,你会得到不太有用的 "undefined is not a function" 错误。
const {p = 1, t = 25, ...otherParams} = request.query
上周五我遇到了同样的错误。