我正在使用API-easy,如何在_id_user中获得此结果并将调用发送到.post()谢谢。 e.g
var APIeasy = require('api-easy'),
assert = require('assert');
var _id_user;
var suite = APIeasy.describe('Test User');
suite.use('localhost', 3000)
.discuss('Test')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.post('/user/authenticate', {data: '{"email":"emailuser@email.com","password":"123456"}')
.expect('should respond with ID user', function (err, res, body) {
_id_user = body; // I need this result to be sent in the next call .post()
}).next()
.post('/user/validate',{ data : _id_user}) // this result always comes null
.expect('should respond TRUE', function (_err, _res, _body) {
}).export(module);
答案 0 :(得分:2)
处理此问题的正确方法是使用before()调用来修改post参数。您可以直接修改“传出”请求的内容。
var APIeasy = require('api-easy'),
assert = require('assert');
var suite = APIeasy.describe('Test User');
suite.use('localhost', 3000)
.discuss('Test')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.post('/user/authenticate', {data: '{"email":"emailuser@email.com","password":"123456"}')
.expect('should respond with ID user', function (err, res, body) {
suite.before('setUserId', function(outgoing) {
//use outgoing.body for post requests and outgoing.uri for get requests
outgoing.body = outgoing.body.replace('_ID_USER',body);
return outgoing;
});
}).next()
.post('/user/validate',{ data : '_ID_USER'})
.expect('should respond TRUE', function (_err, _res, _body) {
//you can unbefore() here if you need it
suite.unbefore('setUserId');
}).export(module);
答案 1 :(得分:-2)
这是因为回调函数的异步性质。在回调函数内执行第二篇文章,即在_id_user=body;