小编pig*_*ack的帖子

Express和ejs <%=呈现JSON

在我的index.ejs中我有这个代码:

var current_user = <%= user %>
Run Code Online (Sandbox Code Playgroud)

在我的节点中我有

app.get("/", function(req, res){
    res.locals.user = req.user
    res.render("index")
})
Run Code Online (Sandbox Code Playgroud)

但是,在我获得的页面上

var current_user = [object Object]
Run Code Online (Sandbox Code Playgroud)

如果我写

var current_user = <%= JSON.stringify(user) %>
Run Code Online (Sandbox Code Playgroud)

我获得:

var current_user = {&quot;__v&quot;:0,&quot;_id&quot;:&quot;50bc01938f164ee80b000001&quot;,&quot;agents&quot;:...
Run Code Online (Sandbox Code Playgroud)

有没有办法传递一个JS可读的JSON ?

javascript json ejs express

72
推荐指数
2
解决办法
6万
查看次数

Passport-twitter:无法在会话中找到请求令牌

就在昨天在Heroku上我开始在twitter上登录快递时出现此错误

Error: failed to find request token in session
    at Strategy.<anonymous> (/app/node_modules/passport-twitter/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth.js:120:54)
    at Strategy.authenticate (/app/node_modules/passport-twitter/lib/passport-twitter/strategy.js:82:40)
    at Passport.authenticate (/app/node_modules/passport/lib/passport/middleware/authenticate.js:153:14)
    at callbacks (/app/node_modules/express/lib/router/index.js:272:11)
    at param (/app/node_modules/express/lib/router/index.js:246:11)
    at pass (/app/node_modules/express/lib/router/index.js:253:5)
    at Router._dispatch (/app/node_modules/express/lib/router/index.js:280:4)
    at Object.handle (/app/node_modules/express/lib/router/index.js:45:10)
    at Context.next (/app/node_modules/express/node_modules/connect/lib/http.js:204:15)
    at Context.<anonymous> (/app/node_modules/passport/lib/passport/context/http/actions.js:64:8)
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

javascript node.js passport.js

21
推荐指数
3
解决办法
1万
查看次数

AngularJS文件拖放指令

这个例子几乎完成了我想在Angular-js中移植的内容:HTML5 File API.

我一直试图谷歌一些指令的例子但是我发现大量使用DOM的老例子或不是为Angular 1.0.4编写的.

基本上这是纯粹的js代码:

var holder = document.getElementById('holder'),
    state = document.getElementById('status');

if (typeof window.FileReader === 'undefined') {
  state.className = 'fail';
} else {
  state.className = 'success';
  state.innerHTML = 'File API & FileReader available';
}

holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
  this.className = '';
  e.preventDefault();

  var file = e.dataTransfer.files[0],
      reader = new FileReader();
  reader.onload = function …
Run Code Online (Sandbox Code Playgroud)

javascript html5 file-upload angularjs angularjs-directive

19
推荐指数
2
解决办法
3万
查看次数

初始化元素在C中不是常量

可能重复:
尝试使用const初始化变量时出错"初始化元素不是常量"

我来自javascript/php/python,可能我错过了什么,这里是代码:

const int a = 50;
const int c = 100;
const int d = 100;
int endX = c + a;
int endY = d;
int startX, startY, b;
Run Code Online (Sandbox Code Playgroud)

我明白了

ex1.4.c:6:错误:初始化元素不是常量
ex1.4.c:7:错误:初始化元素不是常量

有人有解释吗?

c

16
推荐指数
3
解决办法
6万
查看次数

如何以非阻塞方式在Node.js中搜索数组?

我有一个数组是:

[ 4ff023908ed2842c1265d9e4, 4ff0d75c8ed2842c1266099b ]
Run Code Online (Sandbox Code Playgroud)

我必须找到以下内容,是否在该数组中

4ff0d75c8ed2842c1266099b
Run Code Online (Sandbox Code Playgroud)

这是我写的:

Array.prototype.contains = function(k) {
  for(p in this)
     if(this[p] === k)
        return true;
  return false;
}
Run Code Online (Sandbox Code Playgroud)

显然,它不能正常工作,或者更好,但有时它可以工作,但它看起来阻止我.有没有人可以检查那个?

非常感谢

node.js

15
推荐指数
1
解决办法
5万
查看次数

NodeJS和会在导入时给出SyntaxError

我正在使用带有标志child_process的ES6测试节点,--harmony但是在导入时它在第一步失败了.有任何想法吗?

import {'spawn'} from child_process;
console.log(spawn);
Run Code Online (Sandbox Code Playgroud)

我跑:

node --harmony test.js
Run Code Online (Sandbox Code Playgroud)

我得到:

:1
(function (exports, require, module, __filename, __dirname) { import {spawn} f
                                                              ^^^^^^
SyntaxError: Unexpected token import
Run Code Online (Sandbox Code Playgroud)

javascript node.js ecmascript-harmony

14
推荐指数
1
解决办法
9710
查看次数

Mongoose 3.4.0返回[object Object]而不是实际值 - Nodejs

这是我的架构

var elementSchema = new Schema({
  name: String,
  storage: {
    url: String,
    type: String
  }
});
Run Code Online (Sandbox Code Playgroud)

但是当我:

var newElement = new Element();
  newElement.storage = {};
  newElement.storage.url = "asd";
  newElement.storage.type = "asd";
  console.log(newElement.storage);
Run Code Online (Sandbox Code Playgroud)

我明白了[object Object],它应该回归{url:"asd",type:"asd"}

事实上,如果我去查询它,我得到:

{
  storage: "[object Object]",
  bubble: ObjectId("510880b82e6e350200000002"),
  _id: ObjectId("5113c0c0786ece0000000001"),
  __v: 0
}
Run Code Online (Sandbox Code Playgroud)

mongoose node.js

8
推荐指数
1
解决办法
2719
查看次数

在Java中使用相同的列表和流两次

我必须用流完成这个简单的操作:给出一个列表,得到总和和前20个元素的总和.

这就是我的想法

IntStream stream = obj.stream().mapToInt(d->d.getInt());
stream.limit(20).sum() / stream.sum();
Run Code Online (Sandbox Code Playgroud)

但是我不能这样做,因为我被告知我不能重复使用流,所以..我尝试了以下内容:

List<Integer> counts = obj.stream()
    .mapToInt(d -> d.getInt())
    .boxed().collect(Collectors.toList());

counts.stream().limit(20).sum() / counts.stream().sum();
Run Code Online (Sandbox Code Playgroud)

但是我被告知我不能在Stream上使用sum,所以我需要再次mapToInt用于这个简单操作的左右两侧.

有没有办法使用流以更优雅和简洁的方式执行此操作?

java java-8 java-stream

6
推荐指数
2
解决办法
3661
查看次数

Hunchentoot 支持 CORS

我在 hunchentoot 上启用 CORS 时遇到一些问题:

  (hunchentoot:define-easy-handler (one-api :uri *one-endpoint*) () 
    (when (boundp '*acceptor*)
      (setf (hunchentoot:header-out "Access-Control-Allow-Origin") "*")
      (setf (hunchentoot:header-out "Accept") "*/*")
      (setf (hunchentoot:header-out "Access-Control-Allow-Headers") "Content-Type, Accept, Origin") 
      (setf (hunchentoot:header-out "Access-Control-Allow-Methods") "POST, GET, OPTIONS, PUT, DELETE") 
      (setf (hunchentoot:header-out "Access-Control-Allow-Origin") "*") 
      (setf (hunchentoot:content-type*) "text/html"))
    (let* ((raw-data (hunchentoot:raw-post-data :force-text t)))
      (funcall callback raw-data))))
Run Code Online (Sandbox Code Playgroud)

但还是不行,是我哪里做错了吗?

common-lisp hunchentoot

6
推荐指数
1
解决办法
346
查看次数

事件驱动设计c

这是一个小小的理论问题.想象一下装满传感器的设备.现在,如果传感器x检测到某些东西,应该发生一些事情.同时,如果检测到其他东西,例如两个传感器检测到两个不同的东西,则该设备必须表现不同.

从网页设计(所以javascript)我学习了事件,例如(使用jquery)$(".x").on("click", function(){})或angularjs $scope.watch("name_of_var", function()).

是否有可能在C中复制此行为,而不使用复杂的库?

谢谢.

c events event-driven-design

5
推荐指数
2
解决办法
1万
查看次数