小编use*_*162的帖子

如何在Node.js UnhandledPromiseRejectionWarning中找到未处理的承诺?

来自版本7的Node.js具有async/await语法糖用于处理promises,现在在我的代码中经常出现以下警告:

(node:11057) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection (rejection id: 1): ReferenceError: Error: Can't set headers 
after they are sent.
(node:11057) DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled 
will terminate the Node.js process with a non-zero exit code.
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有提到缺少捕获的线.有没有办法找到它而不检查每个try/catch块?

warnings unhandled-exception node.js promise async-await

162
推荐指数
4
解决办法
5万
查看次数

随机化正态分布数字的偏差(javascript)

我在使用JavaScript生成正态分布的随机数(mu = 0 sigma = 1)时遇到问题.

我已经尝试过Box-Muller的方法和ziggurat,但生成的数字序列的平均值为0.0015或-0.0018 - 非常远离零!超过500,000个随机生成的数字这是一个大问题.它应该接近零,类似于0.000000000001.

我无法弄清楚它是否是方法问题,或者JavaScript的内置是否Math.random()产生不完全均匀分布的数字.

有人发现了类似的问题吗?

在这里你可以找到ziggurat功能:

http://www.filosophy.org/post/35/normaldistributed_random_values_in_javascript_using_the_ziggurat_algorithm/

以下是Box-Muller的代码:

function rnd_bmt() {
    var x = 0, y = 0, rds, c;

    // Get two random numbers from -1 to 1.
    // If the radius is zero or greater than 1, throw them out and pick two
    // new ones. Rejection sampling throws away about 20% of the pairs.
    do {
        x = Math.random()*2-1;
        y = Math.random()*2-1;
        rds = x*x + y*y;
    }
    while …
Run Code Online (Sandbox Code Playgroud)

javascript random numbers generator

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

为什么 google Oauth verifyIdToken(javascript nodejs 版本)不使用客户端机密?

我正在为 SPA js+nodejs 应用程序测试 google singin。我添加了这个:

<script src="https://apis.google.com/js/platform.js" async defer></script>
Run Code Online (Sandbox Code Playgroud)

还有这些:

<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<div class="g-signin2" data-onsuccess="onSignIn"></div>
Run Code Online (Sandbox Code Playgroud)

在 html5/js 客户端。遵循本指南:

https://developers.google.com/identity/sign-in/web/sign-in

当用户对库进行身份验证时,将获取令牌并将其传递给服务器,如下所述:

https://developers.google.com/identity/sign-in/web/backend-auth

在服务器端 (nodejs) 使用此函数验证令牌:

client.verifyIdToken(
    token,
    CLIENT_ID,
    // Or, if multiple clients access the backend:
    //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3],
    function(e, login) {
      var payload = login.getPayload();
      var userid = payload['sub'];
      // If request specified a G Suite domain:
      //var domain = payload['hd'];
    });
Run Code Online (Sandbox Code Playgroud)

我的问题是:client_secret 何时使用?因为我使用 CLIENT_ID 前端从谷歌获取身份验证令牌,然后我使用 CLIENT_ID 服务器端进行令牌验证。我认为可以使用仅在服务器端已知的 client_secret(即 SECRET)来验证令牌,因此没有其他人可以获取令牌来验证该用户。我错过了什么?

node.js google-oauth google-api-nodejs-client google-signin

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

couchdb 2.0安装错误

我正在尝试按照以下指南在ubuntu服务器14.04上安装couchdb2.0:http : //docs.couchdb.org/en/2.0.0/install/unix.html https://gist.github.com/SinanGabel/ eac83a2f9d0ac64e2c9d4bd936be9313#file-couchdb-2-0-install-L33 进程启动但它不断发出以下错误:

couchdb@outland:/opt/couchdb/bin$ (./couchdb &)
couchdb@outland:/opt/couchdb/bin$ [info] 2017-01-09T17:26:35.870856Z couchdb@localhost <0.9.0> -------- Application couch_log started on node couchdb@localhost
[info] 2017-01-09T17:26:35.874078Z couchdb@localhost <0.9.0> -------- Application folsom started on node couchdb@localhost
[info] 2017-01-09T17:26:35.901209Z couchdb@localhost <0.9.0> -------- Application couch_stats started on node couchdb@localhost
[info] 2017-01-09T17:26:35.901354Z couchdb@localhost <0.9.0> -------- Application khash started on node couchdb@localhost
[info] 2017-01-09T17:26:35.907365Z couchdb@localhost <0.9.0> -------- Application couch_event started on node couchdb@localhost
[info] 2017-01-09T17:26:35.911510Z couchdb@localhost <0.9.0> -------- Application ibrowse started on node couchdb@localhost
[info] 2017-01-09T17:26:35.915353Z couchdb@localhost …
Run Code Online (Sandbox Code Playgroud)

install couchdb ubuntu-server

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

无法使用n升级Node.js.

我试图在Ubuntu上更新的Node.js serever 14.04使用n. n升级但版本仍然相同:

root@0e2dbaa3db85:/home# node -v
v0.10.25

root@0e2dbaa3db85:/home# n 0.10.33
  install : node-v0.10.33
    mkdir : /usr/local/n/versions/node/0.10.33
    fetch : https://nodejs.org/dist/v0.10.33/node-v0.10.33-linux-x64.tar.gz
installed : v0.10.33

root@0e2dbaa3db85:/home# node -v
v0.10.25
Run Code Online (Sandbox Code Playgroud)

我是否必须告诉n默认使用哪个版本?
我试过n latest,它安装0.12.0但再次node -v输出0.10.25.

我该怎么办?

node.js npm

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

google-auth-library v.1.0如何仅用于身份?

我一直在使用google-auth-library@0.10.0 nodejs来验证我的api服务中的用户身份,现在它已改为1.0,一切都坏了.

我以前用过这里的例子:

https://developers.google.com/identity/sign-in/web/backend-auth

现在我无法弄清楚如何使用新库验证身份.这里的示例:https://github.com/google/google-auth-library-nodejs 解释了如何访问谷歌API,我只需要验证身份.这是我的代码:

 const GoogleAuth = require('google-auth-library');
    const auth = new GoogleAuth.GoogleAuth();
    const google = require('googleapis');
    const authData = {
      'googleAuth': {
        'clientID': 'xxxxxxxxxxx-aaaaaaaaaaaaaaaaaaaaaa.apps.googleusercontent.com',
        'clientSecret': 'sssssssssssssssssssssssss',
        'callbackURL': 'http://localhost:121212/auth/'
      }
    };
const CLIENT_ID = authData.googleAuth.clientID;
    function verifyToken(token) {
      let tokenPromise = new Promise(function(resolve, reject) {
        client.verifyIdToken(
          token,
          CLIENT_ID,
          // Or, if multiple clients access the backend:
          //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3],
          function(e, login) {
            if (e) {
              return reject(e);
            } else {
              var payload = login.getPayload();
              var …
Run Code Online (Sandbox Code Playgroud)

identity google-authentication node.js

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