它不起作用,因为您设置了键
index
dd2数组的索引,
{DD2.map((x, i) => (
<option key={i} value={x.content}>{x.content}</option>
))}
因此当您的DD2数组更改时,该选项仍然相同,因为它具有相同的键。因此,只需将您的钥匙更改为独特的内容即可识别列表中的项目
{DD2.map((x, i) => (
<option key={x.content} value={x.content}>
{x.content}
</option>
))}
您有许多小的(有些不那么小)会导致您的问题。您的主要问题是将tcars* p_2
传递给readbinaryfile()
,并用p_2 = malloc(sizeof(tcars)*(* pc)); 每次。为什么?
每个调用malloc()
返回带有新地址的新内存块。您可以在每个新调用中覆盖p_2
的地址,创建一个内存泄漏并丢失最后一个调用之前存储的数据。相反,您需要realloc()
重新分配较大的内存块,然后将现有数据复制到新的较大块,以便您可以在AS上添加下一个tcars
价值的数据重新分配的块的末端。
如果您是从一个文件读取数据,则无需将p_2
作为一个开始。只需在readbinaryfile()
和realloc()
中声明一个新指针,然后添加每个tcars
末尾的数据,然后返回新分配的新分配的数据指针。 (您必须验证每个分配和重新分配)
您选择的void
for writebinaryfile()
将隐藏遇到的任何错误创建或写入新文件。您必须验证每个输入/输出文件操作,尤其是如果以后将在程序中使用的数据。类型int
返回0
的简单选择,而 1 成功(或vice-vices-vices-vices-vissa,to you you)就是您的全部需要。这样,您可以在文件创建或写作过程中处理任何错误,而不是盲目地承担成功。
一个更细微的问题/错误是您使用ParkingLot1
使用malloc()
的分配。理想情况下,您将使用calloc()
或使用memset
设置所有字节零。为什么?您将整个tcars
struct写入文件。 malloc()
不会初始化分配的内存。这意味着company
名称(nul终止字符)和16
字节的末端之间的所有字符将不限化。虽然这不会引起您的读或写入问题,但要确保将所有数据写入文件的所有数据都是初始化的数据,却更好。否则,检查文件的内容将显示写入文件中的非初始化值的块。
另一个小样式问题是指针声明中的'*'
通常与变量而不是类型有关。为什么?
Tcars* a, b, c;
最上面的声明当然不会声明tcars
的3个分数,而是声明指针a
和两个类型tcars
带有自动存储的结构持续时间b
和c
。写作:
Tcars *a, b, c;
清楚。
最后,不要在功能中使用 magicnumbers 或硬码文件名。您不需要重新编译代码即可读取或编写其他文件名。可以使用“ cars.dat”
作为main()
中的默认文件名,但要么将文件名为第一个参数(这是是什么) int argc,char ** argv
参数到main()
是为)或提示用户获取文件名并将其作为输入。 3
和16
是 magicnumbers 。如果您需要常量,则#define
它们或使用全局enum
。
将其完全放置,您可以做类似的事情,从数据文件中读取未知数的tcars
:(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NCARS 3 /* if you need a constant, #define one (or more) */
#define COMPANY 16
typedef struct Cars {
int years;
char company[COMPANY];
} Tcars;
/* returns 1 on success 0 on error */
int WriteBinaryFile (char *file_name, Tcars *p_1, size_t nelem)
{
FILE *f;
size_t size = sizeof *p_1;
f = fopen (file_name, "wb");
if (!f) { /* validate file open for writing */
perror ("fopen-file_name-write");
return 0;
}
/* validate that nelem blocks of size are written to file */
if (fwrite (p_1, size, nelem, f) != nelem) {
return 0;
}
if (fclose (f)) { /* validate close-after-write */
perror ("fclose-f");
return 0;
}
return 1;
}
/* returns pointer to allocated block holding ncars cars on success,
* NULL on failure to read any cars from file_name.
*/
Tcars *ReadBinaryFile (char *file_name, size_t *ncars)
{
FILE *f;
Tcars *tcars = NULL, temp;
size_t nelem = *ncars, size = sizeof (Tcars);
f = fopen (file_name, "rb");
if (!f) {
perror ("fopen-file_name-read");
return NULL;
}
while (fread (&temp, size, 1, f) == 1) {
/* always realloc to a temporary pointer */
void *tempptr = realloc (tcars, (nelem + 1) * size);
if (!tempptr) { /* validate realloc succeeds or handle error */
perror ("realloc-tcars");
break;
}
tcars = tempptr; /* assign reallocated block */
memcpy (tcars + nelem, &temp, size); /* copy new car to end of block */
nelem += 1;
}
fclose (f);
*ncars = nelem;
return tcars;
}
/* void is fine for print functions with no bearing on the
* continued operation of your code.
*/
void prn_cars (Tcars *cars, size_t nelem)
{
for (size_t i = 0; i < nelem; i++) {
printf ("%4d %s\n", cars[i].years, cars[i].company);
}
}
int main (int argc, char **argv)
{
/* read from filename provided as 1st argument ("cars.dat" by default) */
char *filename = argc > 1 ? argv[1] : "cars.dat";
/* must use calloc() on ParkingLot1 or zero memory to avoid writing
* unintialized characters (rest of company) to file.
*/
Tcars *ParkingLot1 = calloc (NCARS, sizeof(Tcars)),
*ParkingLot2 = NULL;
size_t cars_amount = 0;
if (!ParkingLot1) { /* validate EVERY allocation */
perror ("calloc-ParkingLot1");
return 1;
}
for (int i = 0; i < NCARS; i++) {
ParkingLot1[i].years = 2000 + i;
}
strcpy (ParkingLot1[0].company, "Fiat");
strcpy (ParkingLot1[1].company, "Ford");
strcpy (ParkingLot1[2].company,"Toyota");
/* validate WriteBinaryFile succeeds or handle error */
if (!WriteBinaryFile (filename, ParkingLot1, NCARS)) {
return 1;
}
ParkingLot2 = ReadBinaryFile (filename, &cars_amount);
if (ParkingLot2) { /* validate ReadBinaryFile succeeds or handle error */
prn_cars (ParkingLot2, cars_amount); /* output cars read from file */
free (ParkingLot2); /* free if ParkingLot2 not NULL */
}
free(ParkingLot1); /* free ParkingLot1 */
}
注意:您始终检查的返回fclose()
after-a-write捕获任何文件错误和错误将数据冲入文件,而在fwrite()
呼叫时都无法捕获的文件)
也要注意在男人页面中fread
和fwrite
他们可以读取或写入少于您要求的字节数(或元素)。 A 短阅读可能代表或可能不代表错误或过早的文件结束,您需要调用ferror()
和feof()
确定发生了哪些(如果有)。虽然直接从磁盘读取的文件并不像网络读取和写入那样容易简短阅读,但无论在何处读取或写入数据的位置,完整的实现都会防止简短阅读。进一步调查将留给您。
示例使用/输出
$ ./fwrite_fread_cars dat/cars.dat
2000 Fiat
2001 Ford
2002 Toyota
内存使用/错误检查
在您编写的任何代码中都动态分配内存,您对分配的任何内存块有2个责任 :(1)始终保留一个指向记忆块的起始地址 so,(2)当不再需要时,它可以是 freed 。
必须使用内存错误检查程序来确保您不尝试访问内存或在分配块的边界之外/之外访问内存或写入,试图在非初始化的值上读取或以有条件的跳跃来确认您可以释放所有分配的内存。
对于Linux valgrind
是正常的选择。每个平台都有类似的内存检查器。它们都易于使用,只需通过它运行您的程序。
$ valgrind ./fwrite_fread_cars dat/cars.dat
==7237== Memcheck, a memory error detector
==7237== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7237== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==7237== Command: ./fwrite_fread_cars dat/cars.dat
==7237==
2000 Fiat
2001 Ford
2002 Toyota
==7237==
==7237== HEAP SUMMARY:
==7237== in use at exit: 0 bytes in 0 blocks
==7237== total heap usage: 9 allocs, 9 frees, 10,340 bytes allocated
==7237==
==7237== All heap blocks were freed -- no leaks are possible
==7237==
==7237== For lists of detected and suppressed errors, rerun with: -s
==7237== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
始终确认您已经释放了所有已分配的内存,并且没有内存错误。
看看事情,让我知道您是否有任何疑问。
你肯定可以。我最喜欢的方法是如下:
type NumberLogger = (n: number) => void;
function preloadResources(informLoadPct: NumberLogger )
{
let pct = 100;
informLoadPct(pct);
}
如果您需要在对象内传递此方法,则可以做同样的事情。
type NumberLogger = (n: number) => void;
type NumberLoggerProps = {
logger: NumberLogger;
}
// or
type NumberLoggerProps = {
logger: (n: number) => void;
}
// or
type NumberLoggerProps = {
logger(n: number): void;
}
请参阅 typescript文档的更多信息。
我不知道在全球范围内做到这一点是否会很容易(描述的孔隙函数似乎在注册表级别不起作用)。
在存储库级别上,尽管您可以使用其Terraform中的示例之类的内容: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository_policy_policy
resource "aws_ecr_repository" "example" {
name = "example-repo" # Or replace with a set of repositories/ a data reference
}
data "aws_iam_policy_document" "example" {
statement {
sid = "new policy"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["123456789012"]
}
actions = [
"ecr:DescribeImages"
]
}
}
resource "aws_ecr_repository_policy" "example" {
repository = aws_ecr_repository.example.name
policy = data.aws_iam_policy_document.example.json
}
只需按照此链接使用M1/M2芯片将Java 1.8安装到Mac中。苹果使用 rosetta2 用于Mac向Apple Silicon的Mac Transition。
https://www.oracle.com/java/technologies/downloads/# Java8-Mac
向下滚动至Java 1.8,
转到MacOS版本,
单击M2芯片的ARMX64版本,
遵循入职过程,只需安装M2/M1的Java 1.8即可。
这是因为方法fullname
没有返回任何内容(none
)。要像预期的那样使用该功能,而不是打印字符串,请返回。
def fullname(self):
return f'{self.name} {self.lname}'
使用pd.factorize
与frozenset
df['Val'] = pd.factorize(df[['B', 'C', 'D']].apply(frozenset, axis=1))[0] + 1
print(df)
# Output
A B C D Val
0 AB X 10 1a 1
1 FH Y 15 4h 2
2 KY Z 20 6f 3
3 DA X 10 1a 1
对于Windows:
转到PATH \ ELASTICSERACH-8.2.2 \ bin
文件夹。
在上面的文件夹或路径下打开PowerShell端子。
现在运行此命令:。\ elasticsearch-reset-password -u弹性。
注意 - 不要忘记在最后一个。 。密码-U弹性。
注意 - 弹性搜索应运行
Password for the [elastic] user successfully reset.
New value: ******* (this will be your password)
看来您错过了一个括号:
await http.get(...).then((response) => callback(...))
它们允许您在可用后立即使用上一个将来的结果。如果发现令人困惑,则可以一次声明一个变量。
final response = await http.get(...);
// Check if response was as expected
await callback();
我的主要问题是我有很多管理页面和许多前页面。因此,我不想为此项目提供每个页面解决方案。因此,我认为如果可以使用PathName。我的管理面板路由以“/admin-panel”开头,因此我检查了一下。 _app.js
中的以下逻辑为我工作
function MyApp({ Component, pageProps, router }) {
const adminPanel = router.route.startsWith('/admin-panel') ? true : false
const getLayout =
adminPanel ? ((page) => <AdminLayout children={page} />)
: ((page) => <FrontLayout children={page} />);
return (
<>
{getLayout(<Component {...pageProps} />, pageProps)}
</>
);
注意:此代码是为discord.js v13.7.0编写的,并续集v6
不当更改表
&lt; sequelize&gt; .sync()
未使用Alter或force
sequelize 执行根据他们的V6文档,提供了一个称为sync()
的函数。此功能用于确保您的模型与数据库最新。但是,有一个警告。如果您在没有参数的情况下执行sync()
,则数据库将不会覆盖现有数据。这是您的问题的来源。当您第一次定义模型时,很可能您做以下两件事:
- 定义
id
为datatypes.integer
- id 是一个唯一的字段
由于这些,并且您执行.sync()
而没有参数,因此数据库的表不会被覆盖,因此可以保留旧的唯一字段。另外,如果您尝试将Discord ID存储为整数,则可能会遇到一个问题,即缩短或舍入的DISORD ID。
解决方案
将表
作为一次性修复丢弃,您可以使用此命令手动从sqlite3删除表,然后重新运行机器人,而无需修改sync()
,它将使用正确的数据创建表:
DROP TABLE 'Cooldowns'
修复过时的表格
为了修复过时的表,您有两个选择。但是,要小心,因为这些都是破坏性的。您可以使用以下参数执行sync()
命令:
<Sequelize>.sync({ alter: true }); // Alters tables
<Sequelize>.sync({ force: true }); // Forcefully recreates tables
如前所述,请小心这些操作,因为它们具有破坏性,如果您没有备份,则无法恢复。
正确存储Discord ID,
您需要做的就是将Discord ID存储为datatypes.string
或datatypes.text.text
。这将保留ID的雪花形式并防止缩短。
sequelize.define("User", {
id: {
type: DataTypes.STRING, // Or .TEXT
unique: false
},
// ...
});
有一个名为 dateTimerange 。创建事件后,您可以获取启动和结束DateTime(您已经在执行此操作),并将其传递到DateTimerange函数中,然后将其作为字符串存储在DB中。
当您重新获得活动时。您可以再次获得DateTime值。这是库文档中的一件代码:
import datetime
from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-01-01T00:00:00+0900", "2015-01-04T00:00:00+0900")
for value in time_range.range(datetime.timedelta(days=1)):
print(value)
使用多个键来完成此操作的动态方法:
A dynamic way to do that with MULTIPLE keys:
Use:
如何通过多个字段对对象进行排序?