使用 knex 从数据库中获取数据转义斜杠
getting data from the database escaping slash using knex
我使用Knex 从数据库中获取数据。接收数据时knex escape slashing.
return这个:
{"selector":"a[class=\"xf-product-title__link js-product__title\"]","site":"https:\/\/www.goo.com\/catalog\/kartinki\/nasha/"}
需要:
{"selector":"a[class=\"xf-product-title__link js-product__title\"]","site":"https://www.goo.com/catalog/kartinki/nasha/"}
如何解决这个问题?
我的代码:
let result = new Promise(function (getRes,getErr) {
knex.from('configs')
.select('*')
.where({id:id_config})
.then((ans) => {
getRes(ans);
}).catch((err) => { getErr(err) })
.finally(() => {
knex.destroy();
});
});
return await result;
您混淆了 javascript 字符串文字表示和实际 javascript 对象数据以及 JSON 字符串格式数据:
// store some jsonb data from DB to variable result
> result = null; knex.select(knex.raw(`'{"selector":"a[class=\"xf-product-title__link js-product__title\"]","site":"https://www.goo.com/catalog/kartinki/nasha/"}'::jsonb`)).then(res => result = res)
// data was returned from DB as javascript object
> result
[ { jsonb:
{ site: 'https://www.goo.com/catalog/kartinki/nasha/',
selector: 'a[class="xf-product-title__link js-product__title"]' } } ]
// stringifying data to JSON string requires that " marks are escaped
// but in javascript string literals \ escape mark shows as \ because
// otherwise those could be mixed with \t or \n etc.
> JSON.stringify(result)
'[{"jsonb":{"site":"https://www.goo.com/catalog/kartinki/nasha/","selector":"a[class=\"xf-product-title__link js-product__title\"]"}}]'
// however when javascript string literal is printed to console or written
// to file, those \ marks are outputted as single backslashes
> console.log(JSON.stringify(result))
[{"jsonb":{"site":"https://www.goo.com/catalog/kartinki/nasha/","selector":"a[class=\"xf-product-title__link js-product__title\"]"}}]
我使用Knex 从数据库中获取数据。接收数据时knex escape slashing.
return这个:
{"selector":"a[class=\"xf-product-title__link js-product__title\"]","site":"https:\/\/www.goo.com\/catalog\/kartinki\/nasha/"}
需要:
{"selector":"a[class=\"xf-product-title__link js-product__title\"]","site":"https://www.goo.com/catalog/kartinki/nasha/"}
如何解决这个问题?
我的代码:
let result = new Promise(function (getRes,getErr) {
knex.from('configs')
.select('*')
.where({id:id_config})
.then((ans) => {
getRes(ans);
}).catch((err) => { getErr(err) })
.finally(() => {
knex.destroy();
});
});
return await result;
您混淆了 javascript 字符串文字表示和实际 javascript 对象数据以及 JSON 字符串格式数据:
// store some jsonb data from DB to variable result
> result = null; knex.select(knex.raw(`'{"selector":"a[class=\"xf-product-title__link js-product__title\"]","site":"https://www.goo.com/catalog/kartinki/nasha/"}'::jsonb`)).then(res => result = res)
// data was returned from DB as javascript object
> result
[ { jsonb:
{ site: 'https://www.goo.com/catalog/kartinki/nasha/',
selector: 'a[class="xf-product-title__link js-product__title"]' } } ]
// stringifying data to JSON string requires that " marks are escaped
// but in javascript string literals \ escape mark shows as \ because
// otherwise those could be mixed with \t or \n etc.
> JSON.stringify(result)
'[{"jsonb":{"site":"https://www.goo.com/catalog/kartinki/nasha/","selector":"a[class=\"xf-product-title__link js-product__title\"]"}}]'
// however when javascript string literal is printed to console or written
// to file, those \ marks are outputted as single backslashes
> console.log(JSON.stringify(result))
[{"jsonb":{"site":"https://www.goo.com/catalog/kartinki/nasha/","selector":"a[class=\"xf-product-title__link js-product__title\"]"}}]