我正在尝试编写一个 HTTP 拦截器来向请求添加“访问级别”参数。我试图从可观察的 currentAccessLevel$ 中获取值。但是,可观察对象未正确返回,并且拦截器破坏了整个请求周期。
这个问题已经在这里和这里有了答案,但是,接受的答案似乎在 Angular 8 中不起作用。我不确定我是否做错了什么,或者是否对 Angular 8 中的拦截器进行了某种更改。
我还尝试过使用 switchMap、flatMap 和 mergeMap。
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { AuthService } from 'src/app/services/auth/auth.service';
@Injectable()
export class AccessLevelInterceptor implements HttpInterceptor {
private accessLevel: string;
constructor(private authService: AuthService) { }
// Intercept HTTP Requests
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Get current …Run Code Online (Sandbox Code Playgroud) 我正在使用 HostListener 来检测用户何时重新加载或关闭浏览器窗口。目的是#1 检查当前用户是否拥有记录上的“正在编辑”锁,#2 如果是,则调用可观察对象来更新数据库。(注意:我已经使用 CanDeactivate 实现了用于导航离开组件的锁定系统。HostListener 的使用专门用于重新加载或关闭浏览器窗口)。
根据这个类似的问题,'window:unload'是一个仅同步事件,因此我无法在主机侦听器的主体中使用异步 API 调用。
然而,根据 Mozilla 的说法,所接受的解决方案XMLHttpRequest()正在被弃用。
注意:从 Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27)、Blink 39.0 和 Edge 13 开始,主线程上的同步请求已被弃用,因为它们会对用户体验产生负面影响。
我需要一个更可靠的解决方案。有没有办法在 Angular 中发送同步 api 调用来解决这个限制?或者是否有另一种方法可以确保 API 调用在浏览器重新加载或关闭之前完成?
// @HostListener allows guard against browser refresh, close, etc.
@HostListener('window:unload', ['$event'])
beforeUnloadHander($event) {
// Check if Is Being Edited must be removed
if (this.mustReleaseIsBeingEdited()) {
this.updateIsBeingEditedSub = this.updateIsBeingEdited$(true).subscribe(result => {
return true;
}, err => { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 3.8 中的 RedisCluster 模块连接到 redis 集群。
redis-py-cluster==2.1.0
Run Code Online (Sandbox Code Playgroud)
更新:添加 ssl=True 后,连接现在可以工作了。
然而,仍然抛出一个 AttributeError 是一个问题。这是 rediscluster 模块的一个可能的错误吗?
from rediscluster import RedisCluster
_host = os.getenv('REDIS_HOST')
_port = os.getenv('REDIS_PORT', 6379)
_password = os.getenv('REDIS_PASSWORD')
try:
startup_nodes = [{"host": _host, "port": _port}]
_redis_client = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, ssl=True, password=_password)
except Exception as x:
print(x)
Run Code Online (Sandbox Code Playgroud)
错误信息:
{"event": "Starting gunicorn 20.0.4", "logger": "gunicorn.error", "level": "info", "timestamp": "2020-09-29T18:20:28.031445Z"}
{"event": "Listening at: http://0.0.0.0:8081 (1)", "logger": "gunicorn.error", "level": "info", "timestamp": "2020-09-29T18:20:28.032004Z"}
{"event": "Using worker: sync", "logger": "gunicorn.error", "level": "info", "timestamp": …Run Code Online (Sandbox Code Playgroud) 我将 NodeJS 与 Express 和 PUG 视图引擎一起使用。
我正在尝试检查数组是否包含某个字符串。我尝试过内置的 javascript 方法,例如:
array.includes(str)
array.indexOf(str) > -1.
Run Code Online (Sandbox Code Playgroud)
然而,这些选项似乎都不起作用。如何检查数组是否包含 PUG 中的某个字符串?
if example_array.length > 0
span() Array contains elements! // This works and appears on page
if example_array.includes('example string') // This does not work
span() Array includes example string! // This does not appear on page
Run Code Online (Sandbox Code Playgroud) 我正在使用 Joi 验证库在 NodeJS 12 中创建一个应用程序来验证 API 请求正文。
我遇到了长度超过 16 位的数字数据类型的问题。
我想验证一个长度为 18 位的数字。
验证适用于较小的数字,但不适用于较大的数字。我认为这可能是由于数字大小的限制。
我无法遵循此类似问题中的选定答案,因为该字段无法转换为字符串。它必须被处理并作为一个数字存储在数据库中。
示例值:
999830129121239112
Run Code Online (Sandbox Code Playgroud)
Joi 验证规则
ExampleNumber: Joi.number().integer().max(999999999999999999).allow(null),
Run Code Online (Sandbox Code Playgroud)
错误信息:
{"message":"'ExampleNumber' must be a safe number at body,ExampleNumber","field":"ExampleNumber"}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个 Angular 8 SPA。我有一个使用 Docker Compose 配置构建和运行的 Dockerfile。
配置已经工作了几个星期,但今天,它突然停止工作。我没有对以前的工作做任何改变。
由于模块错误,Angular 应用程序不再编译,并且由于此问题,所有开发现在都被阻止。
error TS2307: Cannot find module 'ngx-cookie-service'.
Run Code Online (Sandbox Code Playgroud)
我试过:
卸载并重新安装 ngx-cookie-service 模块
删除并重新创建 node_modules 和 package-lock.json
运行 npm cache clean
运行 docker system prune 并重建镜像
手动更改 ngx-cookie-service 的版本
确认 node_modules/ngx-cookie-service 目录存在
题:
我还可以采取哪些其他步骤来解决问题?
包.json
{
"name": "client",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test --browsers ChromeHeadlessNoSandbox",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~8.0.0",
"@angular/cdk": "^8.0.1",
"@angular/common": "~8.0.0", …Run Code Online (Sandbox Code Playgroud) 我正在 NodeJS 版本 12 中构建一个 Web 应用程序。我有来自旧 MySQL 数据库的数据。由于旧数据库的编码问题,有几个字段包含无法正确显示的字符。已经有一些类似的问题,但没有一个能解决我的问题。经过尝试,我更接近解决方案,但仍然需要帮助。
\n\n要转换的数据库中的当前值:
\n\nRikuchi Sokuryo\xc3\x8c\xe2\x80\x9ebu [cartographer], \xc3\xa9\xe2\x84\xa2\xc2\xb8\xc3\xa5\xc5\x93\xc2\xb0\xc3\xa6\xc2\xb8\xc2\xac\xc3\xa9\xe2\x80\xa1\xc3\xa9\xc6\x92\xc2\xa8\nRun Code Online (Sandbox Code Playgroud)\n\n所需的新数据库值:
\n\nRikuchi Sokuryo\xcc\x84bu [cartographer], \xe9\x99\xb8\xe5\x9c\xb0\xe6\xb8\xac\xe9\x87\x8f\xe9\x83\xa8\nRun Code Online (Sandbox Code Playgroud)\n\n该问题与此类似问题中描述的相同。但是,接受的答案并没有解决我的问题。我需要编写一个NodeJS将数据库中的数据转换为可读的字符串。
\n\n我也尝试遵循这个类似问题的答案。我知道该值需要首先转换为二进制,然后转换为所需的编码。但是,它不会返回所需的结果。我用 iconv 和 iconv-lite 包尝试了这个。
\n\n尝试 1:
\n\nlet buf = new Buffer(body, \'binary\');\nlet conv = new iconv.Iconv(\'windows-1252\', \'utf8\');\nlet str = conv.convert(buf).toString();\nconsole.log(`original: ${body} output: ${str.toString()}`);\n\n// original: Rikuchi Sokuryo\xc3\x8c\xe2\x80\x9ebu [cartographer], \xc3\xa9\xe2\x84\xa2\xc2\xb8\xc3\xa5\xc5\x93\xc2\xb0\xc3\xa6\xc2\xb8\xc2\xac\xc3\xa9\xe2\x80\xa1\xc3\xa9\xc6\x92\xc2\xa8\n// output: Rikuchi Sokuryo\xd0\x9cbu [cartographer], \xd0\xb9"\xd1\x91\xd0\xb5S\xc2\xb0\xd0\xb6\xd1\x91\xc2\xac\xd0\xb9!\xd0\x8f\xd0\xb9\xe2\x80\x99\xd0\x81\nRun Code Online (Sandbox Code Playgroud)\n\n尝试 2: iconv-lite
\n\nlet buf = new Buffer(body, \'binary\');\nconst str = iconvlite.decode(buf, …Run Code Online (Sandbox Code Playgroud) 我有一个结构有些复杂的反应形式。我正在努力为表单中的表单组对象数组创建表单控件和验证。
类似的问题之前曾在这里被问过,但我无法找到有关如何绑定到表单组数组的任何答案。
父表单组Record具有顶级属性,然后是表单组对象的嵌套数组Items。
每个项目都有属性“标题”和“描述”。标题和描述需要控制和验证。我不一定知道表单上需要多少个项目,这取决于使用默认值初始化表单时每个“记录”有多少个“项目”。
这是一个 stackblitz:https://stackblitz.com/edit/angular-nkhgsj
以下是代码中的一些片段:
示例数据:
// Placeholder to emulate data from an API response
recordAPIResponse =
{
"ID": 12345,
"Items": [
{
"Title": "Example title 1",
"Description": "Example description 1"
},
{
"Title": "Example title 2",
"Description": "Example description 2"
},
{
"Title": "Example title 3",
"Description": "Example description 3"
}
]
};
Run Code Online (Sandbox Code Playgroud)
该表单使用从 API 响应返回的记录值进行初始化。
private initRecordForm(record: Record) {
// Create items controls with validations
const itemsCtrls = this.createItemsCtrls(record.Items);
const recordForm …Run Code Online (Sandbox Code Playgroud) 我正在创建一个 python 脚本来查询 SQLite DB 文件。我已连接到 SQLite db 文件并运行 SELECT 查询来检索数据。
问题是输出缺少列名。
import sqlite3 as lite
import sys
import json
con = lite.connect('example.db')
with con:
con.row_factory = lite.Row
cur = con.cursor()
cur.execute("SELECT * FROM Devices")
rows = cur.fetchall()
rowarray_list = []
for row in rows:
t = (row['id'], row['name'], row['type'])
rowarray_list.append(t)
j = json.dumps(rowarray_list)
rowarrays_file = 'rowarrays.js'
f = open(rowarrays_file,'w')
print (f, j)
Run Code Online (Sandbox Code Playgroud)
这是脚本的输出。
<_io.TextIOWrapper name='rowarrays.js' mode='w' encoding='UTF-8'> [[1, "example1", "Computer"], [2, "example2", "Server"], [3, "example3", "Server"]
Run Code Online (Sandbox Code Playgroud)
以下是所需的输出。
{"records":[{"id": "1", …Run Code Online (Sandbox Code Playgroud) 我需要在 Javascript (不是 JQUERY)中创建一个正则表达式。
正则表达式必须仅匹配 1 到 11 之间的数字。
我开始使用这个正则表达式来允许数字 1 到 11,但它也允许 21、31、41、51 等。我只是需要数字 1 到 11。谢谢。
var range = /([0-9][0-1]{1,2})/g;
Run Code Online (Sandbox Code Playgroud)
澄清:我需要使用正则表达式进行表单验证。我正在使用 HTML 属性“min/max”,但我正在使用的平台仍然提交表单,因此我需要在提交到服务器之前在客户端捕获它。
我正在使用 Node:12 基础映像构建 Docker 映像。由于图像尺寸较小,我尝试将其切换为使用 Node:12-alpine。我在 Alpine 中安装了 bash 和 Shadow 以便能够运行chmod命令。
我在使用其中一个 RUN 命令时遇到错误RUN chmod +755。错误信息:chmod: invalid mode '+755'
请注意,这适用于 Node:12 基础映像,所以我想我只需要在 Alpine linux 中安装另一个包?谢谢!
FROM node:12.8-alpine
# Create working directory
RUN mkdir -p /home/node/app
# Set working directory
WORKDIR /home/node/app
# Install bash and shadow for permissions chmod commands
RUN apk add --no-cache bash && apk add shadow
# Add `/home/node/app/node_modules/.bin` to $PATH
ENV PATH /home/node/app/node_modules/.bin:$PATH
# Copy code
COPY --chown=node . /home/node/app …Run Code Online (Sandbox Code Playgroud) 我需要从字符串中删除额外的下划线.条件是它们仅出现在字符串的最后.
例如,我们需要将DELL_和DELL__更改为DELL.
我正在考虑使用str.replace但我需要特别匹配案例,如果它出现在字符串的末尾而不是所有出现在该字符串中.另外,我只想在检测到额外下划线时运行此脚本.
我需要有一些逻辑,如IF(hasExtraUnderscores){删除额外的下划线}
我怎么能用javascript做到这一点?
注意:我们无法使用JQuery,如果可能,需要在本机javascript中执行此操作.
angular ×4
javascript ×3
node.js ×3
docker ×2
python ×2
regex ×2
string ×2
alpine-linux ×1
angular8 ×1
database ×1
encoding ×1
express ×1
interceptor ×1
joi ×1
json ×1
npm ×1
numbers ×1
object ×1
observable ×1
pug ×1
redis ×1
rxjs ×1
sqlite ×1
synchronous ×1
validation ×1