React
Checkbox onChange not available
The checkbox onChange function was called, but the value was not changed.
Solution
For most input components in React, text, select… we can get the value from e.target.value (onChange(e)).
But for checkbox, the e.target.value was on by default.
The real value we should care about is e.target.checked.
Database
String cannot match because of the invisible characters
I have a column, with the value of abc. In pgAdmin 4, I can get the record by WHERE column = ‘abc’. But in Node.js, after selected from database and compared by ===, the field was not equal to ’abc’.
That is because the data in database contains an invisible character.
I rewrite the value in pgAdmin 4 but it didn’t work.
Solution
This is often caused by copying text from other files into database.
For PostgreSQL, I used following queries to remove the invisible characters:
-- Select and check the values first
SELECT column_name, length(column_name), octet_length(column_name)
FROM table_name WHERE length(column_name) <> octet_length(column_name);
-- Update features
-- UPDATE table_name
-- SET column_name = regexp_replace(column_name, '[\u200B-\u200D\uFEFF]', '', 'g')
-- WHERE site_field_key ~ ‘abc’;
Related Platforms
(Github Issues) / (Notion) / (Blog)
Advertisement
(A website to optimize your resume and to boost your career: Nonpareil.me)
React
Checkbox的onChange无效
onChange函数被调用了,但是里面的代码修改value没有生效。
解决方案
React中的多数input组件,比如text,select,都可以通过e.target.value (onChange(e)) 来获取它变化后的值。
只有checkbox不一样。Checkbox的e.target.value默认是on。
真正需要关注的值是e.target.checked。
数据库
字符串匹配不成功,因为含有不可见字符
我在数据库中有一个字段,其中的值是abc。在pgAdmin 4客户端中,我可以通过SELECT查到这个字段 WHERE column = ‘abc’。
但是在Node.js中,我从数据库查询到这个字段之后,再用===做比较,就无法匹配了。
这是因为在数据库中的源数据里有一个不可见字符。我在pgAdmin 4客户端中重写这个字段也是无效的。
解决方案
一般来说,造成这个问题的起因,都是因为在数据库中输入值的时候,是从其它地方复制过来的。
对于PostgreSQL,下述语句可以移除不可见字符:
-- 先通过select来确认一下
SELECT column_name, length(column_name), octet_length(column_name)
FROM table_name WHERE length(column_name) <> octet_length(column_name);
-- 更新字段
-- UPDATE table_name
-- SET column_name = regexp_replace(column_name, '[\u200B-\u200D\uFEFF]', '', 'g')
-- WHERE site_field_key ~ ‘abc';
相关平台
(Github Issues) / (Notion) / (Blog)