2017-08-07 49 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