处理JSON对象异常
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
const checkJson = str => {
|
||||
if (typeof str == 'object') {
|
||||
try {
|
||||
str = JSON.stringify(str)
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (typeof str == 'string') {
|
||||
try {
|
||||
let obj = JSON.parse(str);
|
||||
|
||||
@@ -25,8 +25,10 @@ const insert = ({table, form}) => {
|
||||
if (form.id) {//编辑
|
||||
let arr = Object.keys(form).filter(e => form[e]).map(e => {
|
||||
if (typeof form[e] == "object") {
|
||||
if (checkJson(form[e])) form[e] = JSON.stringify(form[e]).replace(/"'/g, "\'")
|
||||
else form[e] = JSON.stringify(form[e])
|
||||
if (checkJson(form[e])) {
|
||||
form[e] = JSON.stringify(form[e]).replace(/["']/g, "\\'")
|
||||
console.log(form[e])
|
||||
} else form[e] = JSON.stringify(form[e])
|
||||
}
|
||||
return `${e}='${form[e]}'`
|
||||
})
|
||||
@@ -37,7 +39,7 @@ const insert = ({table, form}) => {
|
||||
if (form[e]) {
|
||||
cols.push(e)
|
||||
if (typeof form[e] == "object") {
|
||||
if (checkJson(form[e])) form[e] = JSON.stringify(form[e]).replace(/"'/g, "\'")
|
||||
if (checkJson(form[e])) form[e] = JSON.stringify(form[e]).replace(/["']/g, "\\'")
|
||||
else form[e] = JSON.stringify(form[e])
|
||||
}
|
||||
arr.push(`'${form[e]}'`)
|
||||
|
||||
Reference in New Issue
Block a user