从Node.js上的mysql结果JSON获取一个Array值

时间:2022-10-26 10:32:02

I have a mysql column contain Array in it. And when printing this row that contain column in console.log(collection[0]) The result is:

我有一个mysql列包含数组。当在console.log(collection [0])中打印包含列的这一行时,结果是:

    RowDataPacket {
  id: 6,
  title: 'fsd',
  createddate: '2016-04-14 00:16:29',
  tag: 'fsdsd',
  img1: '[\'12071331_184535658548108_1612982316_n.jpg\', \'12071715_826061364175214_103377995_n.jpg\', \'12076929_10207206326744146_1386812779_n.jpg\']',
  img1big: '[\'small_12071331_184535658548108_1612982316_n.jpg\', \'small_12071715_826061364175214_103377995_n.jpg\', \'small_12076929_10207206326744146_1386812779_n.jpg\']' }

and i want to print the value 12071331_184535658548108_1612982316_n.jpg to view. I used a method like this:

我想打印值12071331_184535658548108_1612982316_n.jpg来查看。我使用了这样的方法:

{% for i in collection %}     
 {{i.img1[0]}}
{% endfor %}

But the view showed only first character of a string:

但视图只显示字符串的第一个字符:

[

The result of {{i.img1}} is :

{{i.img1}}的结果是:

['12071331_184535658548108_1612982316_n.jpg', '12071715_826061364175214_103377995_n.jpg', '12076929_10207206326744146_1386812779_n.jpg']

The question is, how can i print 12071331_184535658548108_1612982316_n.jpg on the view? P/s: I tried to use both JSON.parse() and JSON.stringify() for some cases in both controller and view, but no uses. Thanks

问题是,我如何在视图上打印12071331_184535658548108_1612982316_n.jpg? P / s:我试图在控制器和视图中使用JSON.parse()和JSON.stringify()两种情况,但没有用处。谢谢

2 个解决方案

#1


1  

The value for collection[0] seems to be a string. Somewhere in your code you'll need to parse the string into an object (JSON.parse(string)).

collection [0]的值似乎是一个字符串。在代码中的某处,您需要将字符串解析为对象(JSON.parse(string))。

#2


0  

If you want display your image on a view, what you need is not the image name but the image path.

如果要在视图上显示图像,则所需的不是图像名称而是图像路径。

For example, if you store all images under public/images folder. You can use <img src="/images/{{i.img1}}"/> to display your photos.

例如,如果将所有图像存储在public / images文件夹下。您可以使用从Node.js上的mysql结果JSON获取一个Array值来显示照片。

#1


1  

The value for collection[0] seems to be a string. Somewhere in your code you'll need to parse the string into an object (JSON.parse(string)).

collection [0]的值似乎是一个字符串。在代码中的某处,您需要将字符串解析为对象(JSON.parse(string))。

#2


0  

If you want display your image on a view, what you need is not the image name but the image path.

如果要在视图上显示图像,则所需的不是图像名称而是图像路径。

For example, if you store all images under public/images folder. You can use <img src="/images/{{i.img1}}"/> to display your photos.

例如,如果将所有图像存储在public / images文件夹下。您可以使用从Node.js上的mysql结果JSON获取一个Array值来显示照片。