0

我正在使用 API-easy ,如何在 _id_user 中获得此结果并将调用发送到 .post() 谢谢。例如

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);
4

2 回答 2

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);
于 2013-07-19T09:15:51.777 回答
-2

这是因为回调函数的异步性质。在回调函数中做第二篇文章,即之后_id_user=body;

于 2012-10-11T01:19:28.323 回答