JavaScript JSON解析查询与MongoDB

huangapple go评论114阅读模式
英文:

javascript json parsing query with mongodb

问题

I have a function which gets data from my mongodb database. I am trying to console log the result. I can see the result as

  1. console.log(result);

This gives

  1. [
  2. {
  3. _id: 5f2940b77e6f8b26e0726cb9,
  4. Val_String: '2',
  5. T1_String: '2162',
  6. T2_String: '2068',
  7. T3_String: '1950',
  8. T4_String: '1956',
  9. Pdiff_String: '-0.010000',
  10. Time_String: '2020-05-20 12:51:20 GMT',
  11. __v: 0
  12. }
  13. ]

However, when I do:

  1. var T1 = result.T1_String;
  2. console.log(T1);

I get undefined. So my question is how do I correctly parse this to get my data.

英文:

I have a function which gets data from my mongodb database. I am trying to console log the result. I can see the result as

  1. console.log(result);

This gives

  1. [
  2. {
  3. _id: 5f2940b77e6f8b26e0726cb9,
  4. Val_String: '2',
  5. T1_String: '2162',
  6. T2_String: '2068',
  7. T3_String: '1950',
  8. T4_String: '1956',
  9. Pdiff_String: '-0.010000',
  10. Time_String: '2020-05-20 12:51:20 GMT',
  11. __v: 0
  12. }
  13. ]

However, when I do:

  1. var T1 = result.T1_String;
  2. console.log(T1);

I get undefined. So my question is how do I correctly parse this to get my data.

答案1

得分: 1

你需要访问列表中的第一个对象。因此,

  1. var T1 = result[0].T1_String;
  2. console.log(T1);

将会输出 t1 字符串。

英文:

you have to access the first object in the list. Therefore

  1. var T1 = result[0].T1_String;
  2. console.log(T1);

will give the t1 string

答案2

得分: 1

结果返回一个对象数组,而不仅仅是一个单一对象。
通过方括号表示法 [] 访问这些对象。

  1. var T1 = result[0].T1_String; //result[0] 是数组中的第一个对象
英文:

The result gives back an array of objects and not just a single object.
Access the objects by square bracket notation []

  1. var T1 = result[0].T1_String; //result[0] is the first object in the array

huangapple
  • 本文由 发表于 2020年8月5日 16:52:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定