小编tki*_*m90的帖子

How does join() produce different results depending on the arguments?

I can't quite understand why the join() call below produces different results, depending on the type of argument(s) provided.

Here's what I found:

var test = function() {
  var args = Array.prototype.join.call(arguments,"_");
  return args
};

console.log(test([1,2,3])) // #1: returns 1,2,3
console.log(test(1,2,3)) // #2: returns 1_2_3
Run Code Online (Sandbox Code Playgroud)

Given join(arguments, '_'), shouldn't it produce a _ delimited string in both tests above? Why does #1 return a comma delimited value instead?

javascript function

15
推荐指数
2
解决办法
882
查看次数

我可以使用 Cognito 访问令牌生成 ID 令牌吗?

是否可以使用 Cognito 访问令牌生成 ID 令牌?我在网上找不到任何关于此的文档。

我正在尝试获取带有自定义声明的 ID 令牌,但现有的解决方案不适用于我的情况(详细信息请参见此处)。作为一种解决方法,我正在考虑在用户登录后直接使用访问令牌手动向 Cognito 请求 ID 令牌。

我尝试过的

  • 调用 Cognito 的/oauth2/userinfo端点仅返回基本声明,而不是我通过 lambda 触发器添加的自定义声明pre token generation
  • 将自定义声明/属性添加到访问令牌。好像不支持这个。
  • 我还没有探索过的想法:使用 Amplify 并以某种方式通过那里获取 ID 令牌?

amazon-web-services amazon-cognito

7
推荐指数
1
解决办法
3285
查看次数

当用户未经身份验证时反应闪烁的专用路由

我用来react-router-dom保护整个应用程序。所有路由都受到 ProtectedRoute 组件的保护(请参阅下面的代码),该组件会重定向到外部 url,如果用户未登录,则为单点登录 (SSO) 页面。

问题:

当用户转到“/home”时,他们会短暂浏览(“闪现”)受保护的路由,然后重定向到“external-login-page.com/”(登录页面)。如何避免闪烁,使用户只能看到登录页面?

export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
  isAuthenticated,
  ...rest
}) => {
  if (!isAuthenticated) { // redirect if not logged in
    return (
      <Route
        component={() => {
          window.location.href = 'http://external-login-page.com/';
          return null;
        }}
      />
    );
  } else {
    return <Route {...rest} />;
  }
};
Run Code Online (Sandbox Code Playgroud)

reactjs react-router react-router-dom

5
推荐指数
1
解决办法
2874
查看次数

Why is useState not updating state with the keydown handler?

I'm trying to create a simple React image slider, where the right/left arrow keys slides through the images.

Problem

When I press the right arrow ONCE, it works as expected. The id updates from 0 to 1, and re-renders the new image.

When I press the right arrow a SECOND time, I see (through console.log) that it registers the keystroke, but doesn't update the state via setstartId.

Why?

Also, I am printing new StartId: 0 in the component function …

reactjs react-hooks

5
推荐指数
1
解决办法
1673
查看次数

作为客户端应用程序,我如何从 Amazon 负载均衡器 (ALB) 获取转发给我的“x-amzn-oidc-*”标头?

我计划设置 ALB (Amazon Load Balancer) 以进行身份​​验证。它会坐在我的客户端应用程序,并与和的access_token用户要求的智威汤逊页眉只转发认证请求前,x-amzn-oidc-accesstoken+x-amzn-oidc-data分别为[0]。

我的客户端应用程序需要捕获这些转发的标头并将它们存储在 localStorage 中,以便在以后的请求(例如 S3)中访问其他 AWS 资源。

如果我是接收 ALB 请求的客户端应用程序,我将如何拦截这些标头?他们是作为请求还是响应进来的?

[0] 参见图中的第 10 步:https : //www.exampleloadbalancer.com/auth_detail.html

reverse-proxy http amazon-web-services http-headers

4
推荐指数
1
解决办法
1426
查看次数

如何从 textarea 元素中删除调整大小的角?

我正在创建一个简单的文本编辑器 - 文本输入是一个 textarea 元素。我想删除用于更改框大小的两行(实际上,如果可能,我想修复框大小,使其无法更改)。

这就是我要说的: 在此处输入图片说明

编辑:这是代码:

// HTML
<textarea id="canvas" placeholder="Write something..."></textarea>

// CSS
#canvas {
  border: 1px dashed #999;
  background: transparent;
  width: 500px;
  height: 400px;
  margin: 0 auto;
  padding: 5px;
  font-size: 20px;
}
Run Code Online (Sandbox Code Playgroud)

html css

3
推荐指数
1
解决办法
1966
查看次数

从头开始实现reduce(),不知道JS如何知道“数组”是什么

我正在尝试从头开始实现reduce()。我能够让它工作,但是即使我从未在任何地方定义过它,javascript如何知道“数组”是什么?

function reduce(callback, initialVal) {
  var accumulator = (initialVal === undefined) ? undefined : initialVal;
  for (var i=0; i<array.length; i++) {
    if (accumulator !== undefined) {
      accumulator = callback(accumulator, array[i], i, array);
    } else {
      accumulator = array[i]
    }
  }
  return accumulator;
};

// testing a basic sum
arr = [1,2,3]
arr.reduce( (accumulator, elem) => accumulator+=elem )
Run Code Online (Sandbox Code Playgroud)

编辑:我开始工作了:DI 将“array”更改为“this”,因为我在 Array.prototype 下创建了一个新方法。

Array.prototype.myReduce = function(callback, initialVal) {
  var accumulator = (initialVal !== undefined) ? initialVal : undefined;
  for (var i=0; i<this.length; …
Run Code Online (Sandbox Code Playgroud)

javascript reduce

3
推荐指数
1
解决办法
4208
查看次数

如何强制事件回调等待异步调用解决?

我有一个监听名为 的事件的表单onBeforeSubmit。当我提交表单时会触发此事件,但如果回调返回 ,则会停止提交false

问题

我的回调涉及对 API 的异步调用。在决定是否返回 false 之前,如何强制事件等待承诺解决?目前它不等待解决fetch()- 我已经尝试了两种async/await方法.then()......

我尝试过的:

尝试1:

onBeforeSubmit: async (e) {
  const res = await fetch(url);
  // if res value fails some condition
  return false
}
Run Code Online (Sandbox Code Playgroud)

尝试2:

onBeforeSubmit: (e) {
  fetch(url).then( res => {
  // if res value fails some condition
  return false
  })
}
Run Code Online (Sandbox Code Playgroud)

难道不应该await强制 javascript 等待 fetch 解析后再继续下一行吗?

我还注意到,每当我输入关键字时async,它都会忽略该return false语句并继续提交。是什么原因造成的?

编辑:这是第 3 方 API,因此我无法控制 onBeforeSubmit 的行为:(

javascript asynchronous promise dom-events async-await

1
推荐指数
1
解决办法
1656
查看次数

如何从SQL中减去条件和的Total

我想做以下事情:

1)查找表中
的总行数2)查找满足特定条件的总行数.

3)从(2)中减去(1).

样本表员工:

    EmployeeID  Nationality
    1           Brazil
    2           Korea
    3           Germany
    4           Brazil
    5           Brazil
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

    SELECT count(EmployeeID) as Total from Employees
    UNION
    SELECT count(EmployeeID) as Brazilians from Employees
    WHERE Nationality = 'Brazil'
Run Code Online (Sandbox Code Playgroud)

结果:

Total
5
3
Run Code Online (Sandbox Code Playgroud)

第1行将给我总雇员.第2行将给我巴西员工.我使用UNION来查看是否可以从第1行中减去第2行.

我可以使用CASE和SUM()来做这个,但这需要row_number()函数,我不能使用它,因为我正在使用WebSQL.是否有另一种方法可以索引这些行以便能够减去?

我可以使用另一种方法来解决这个看似简单的问题吗?

sql

0
推荐指数
1
解决办法
67
查看次数