2017-08-07 66 views
2

我只是尝试了异步与request-promise/AWAIT跑进此错误:请求 - 无极抛出使用异步“没有定义身份验证机制” /伺机

RequestError: Error: no auth mechanism defined 
     at new RequestError (node_modules/request-promise-core/lib/errors.js:14:15) 
     at Request.plumbing.callback (node_modules/request-promise-core/lib/plumbing.js:87:29) 
     at Request.RP$callback [as _callback] (node_modules/request-promise-core/lib/plumbing.js:46:31) 
     at self.callback (node_modules/request/request.js:188:22) 
     at Auth.onRequest (node_modules/request/lib/auth.js:133:18) 
     at Request.auth (node_modules/request/request.js:1360:14) 
     at Context.<anonymous> (test/routes.js:37:41) 
    From previous event: 
     at Request.plumbing.init (node_modules/request-promise-core/lib/plumbing.js:36:28) 
     at Request.RP$initInterceptor [as init] (node_modules/request-promise-core/configure/request2.js:41:27) 
     at new Request (node_modules/request/request.js:130:8) 
     at request (node_modules/request/index.js:54:10) 
     at Context.<anonymous> (test/routes.js:37:24) 

这是我最近这建立了一个API端点应该在MongoDB中创建一个新用户。它使用Passport策略提供的基本身份验证,并且我已经与Postman一起测试了它的工作原理。我不确定为什么这个错误被抛出。 (使用摩卡)

我的请求代码:

it("creates a new user", async() => { 
    const options = { 
    method: "POST", 
    uri: `http://localhost:${process.env.PORT}/api/users`, 
    headers: { 
     "User-Agent": "Request-Promise", 
     "Content-Type": "application/json" 
    }, 
    body: { 
     email: "[email protected]", 
     password: "password", 
     firstName: "John", 
     lastName: "Smith" 
    }, 
    json: true 
    }; 
    const resp = await request(options).auth(APP_ID, SIGNATURE, false); 
    expect(resp).to.be.an("object"); 
}); 

编辑:我也许应该补充一点,我使用的节点8.2.1和5.3.0 NPM。

回答

0

这通常是由于没有提供合适的证书导致的。引发错误的代码可以在here找到。你有没有在你的测试中验证APP_IDSIGNATURE不是undefined

+0

是的,我确定他们不是。我从事这个项目已经有一段时间了,但我相信'APP_ID'和'SIGNATURE'被定义为文件顶部的模块范围的常量。我使用文字测试,以确保。 – spicypumpkin