发现请求自定义数据的最佳方法是在路由中使用 getCustomData() 方法,将数据设置为变量并使用来自客户端的 get 请求来请求此数据,如下例所示:
服务器 Js
customData = new Array;
router.get("/string", function(req, res) {
req.user.getCustomData(function(err, data) {
customData = data.someKey;
})
res.send(customData) /*Returns Empty Arrray*/
})
router.get("/cdata", function(req, res) {
res.send(customData) /*Sends Actual Custom Data*/
})
客户端JS
var customData = new array
$(document).ready(function() {
$.get("/string", function(string) {
/*Tells server to get customData*/
})
$(document).click(function(){
if(pleaseCount===0){
$.get("/cdata", function(data) {
for(i=0;i<data.length;i++){
customData.unshift(data[i])
pleaseCount=pleaseCount+1
/*Custom data is now stored in client side array*/
}
})
}
})
无论如何,这对我有用。我不知道为什么有人对这个问题投了反对票,因为这是通过在渲染函数中使用 userName:req.user.userName 并将此信息呈现给 p 来检索其他用户信息(例如姓名和电子邮件)的可接受方式。使用 p #{userName} 在翡翠页面中添加标签