简体   繁体   English

如何使用Javascript从字符串中间提取然后重新格式化文本?

[英]How can I extract and then reformat text from the middle of a string with Javascript?

I have named the constraints in my database like "FK_ChildParent". 我在数据库中将约束命名为“ FK_ChildParent”。 When I have a foreign key error caused by an admin person trying to delete a row with children then I get messages like this: 当我因管理员试图删除带有孩子的行而导致外键错误时,我收到如下消息:

exceptionMessage=The DELETE statement conflicted with the REFERENCE constraint "FK_ContentSubject". The conflict occurred in database "xx", table "dbo.Content", column 'SubjectId'.
The statement has been terminated.

Is there an easy way that I can translate this message into something like: 有没有一种简单的方法可以将此消息翻译成类似以下内容的内容:

"Delete failed: Row has Content"

I would really like to find a one line solution but I am not sure if this is possible. 我真的很想找到一种解决方案,但不确定是否可行。

When you get a MySQL constraint error like this, it means there is a problem in your code. 当您收到这样的MySQL约束错误时,表明您的代码有问题。 Your code is trying to do something it shouldn't. 您的代码正在尝试执行不应执行的操作。 You're trying to rely on MySQL to tell you what your code did wrong, but MySQL has no idea because it just knows about tables and fields and constraints. 您试图依靠MySQL来告诉您代码的错误之处,但是MySQL不知道,因为它只知道表,字段和约束。

If your desired error message is something like: "Can't delete folder, there are still files in the folder" ; 如果您想要的错误消息是这样的: “无法删除文件夹,文件夹中仍然有文件” ie. 即。 an fk exists for a record you're trying to delete. 您尝试删除的记录存在fk。 You wont be able to get it by some clever string replacement, especially if the error messages get more complex or you might have multiple queries per request. 您将无法通过一些巧妙的字符串替换来获取它,特别是如果错误消息变得更加复杂,或者每个请求可能有多个查询。

You have to check the foreign key constraint yourself, because it's tied to some implicit business rule ; 您必须亲自检查外键约束,因为它与某些隐式 业务规则相关 ie. 即。 you can't delete a folder if there are files in it. 如果文件夹中有文件,则无法删除。 Test the business rule, if it fails you'll know exactly what went wrong. 测试业务规则,如果失败,您将确切知道出了什么问题。

The MySQL-level constraint just helps you to make sure your data has some semblance of consistency, but don't rely on it. MySQL级别的约束仅可帮助您确保数据具有某种程度的一致性,但不必依赖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM