简体   繁体   English

车把,JS和节点

[英]Handlebars, JS and Node

I'm trying to get hold of the data sent by the server from a MongoDB. 我正在尝试获取服务器从MongoDB发送的数据。 A regular GET: 常规GET:

.get('/test', function(req, res){
  mongo.collection('collection').find({ _id:req.user._id.toString()
    }).toArray( function(err, doc){
        res.render('partials/map', 
          { docs : doc });
      }
    });      
});

Ie sending an array with the documents to the client. 即发送带有文档的数组到客户端。 Then I want to manipulate the data on the client side so I, in the javascript on the client do: 然后,我想在客户端上操作数据,所以我在客户端上的javascript中执行以下操作:

<script>
  var docs = '{{docs}}';
  console.log(typeof(docs));
  console.log(docs);

  var obj = new Object(docs);
  console.log(obj);

  var arr = new Array(docs);
  console.log(arr);

  console.log(JSON.stringify(docs)); 

</script>

However, I can't figure out how to actually manipulate it as the above just gives me the following outputs (in the console): 但是,我无法弄清楚如何进行实际操作,因为上述操作仅在控制台中提供了以下输出:

string
[object Object],[object Object],[object Object]
String {"[object Object],[object Object],[object Object]"}
["[object Object],[object Object],[object Object]"]
"[object Object],[object Object],[object Object]"

How can I manipulate the data? 如何处理数据? I know it's an array with three documents but just trying docs[0] gives the first character in [object Object] (ie "["). 我知道这是一个包含三个文档的数组,但是仅尝试docs [0]会在[object Object]中给出第一个字符(即“ [”)。 Also, a JSON.parse(docs) just returns error as docs somehow already is an object. 另外,JSON.parse(docs)只会返回错误,因为docs已经以某种方式已经是一个对象。

Stringify the object on the server and then access it on the client. 在服务器上对对象进行字符串化,然后在客户端上对其进行访问。

//server
res.render('partials/map', {docs: JSON.stringify(doc)})

//client
var docs = {docs}

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

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