Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions lib/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,29 @@ Cloud.run = function(name, data, options) {
}).then(function(user) {
user = user || options.user;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前在当前进程里调用的时候,不会使用当前全局的 currentUser,这个应该算 bug 还是 feature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feature 呀,从 1.0 之后就没有全局的 currentUser 了

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在想在本地单元测试里,直接调用云函数进行测试,这样就需要一直指定当前 user,感觉比较奇怪。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有全局的 currentUser 当然就只能这么做了

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的啊,但是这样有些奇怪,即时我主动开启 currentUser,也是还是需要显式指定的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在也是没办法主动开启全局 currentUser 了吧,或者说开了就完全没法用了(因为现在没有 domain 了)。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SDK 作为客户端的话,是会使用的,因为会使用当前用户的 session token 来发送请求。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有道理,当时确实是这么说的。所以应该在 Node SDK 所有用到用户的地方检查全局的 currentUser 有没有开启,就像在 JS SDK 里一样

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,不过和这个 PR 没什么关系,我测试了发现没问题,可以合并了。


return new Promise(function(resolve, reject) {
var request = utils.prepareRequestObject({
user: user,
params: data,
req: options.req
});

var response = utils.prepareResponseObject(options.req && options.req.res, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
const cloudFunction = Cloud.functions[name];

Cloud.functions[name](request, response);
const request = utils.prepareRequestObject({
user: user,
params: data,
req: options.req
});

if (cloudFunction.length === 2) {
return new Promise( (resolve, reject) => {
const response = utils.prepareResponseObject(options.req && options.req.res, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});

cloudFunction(request, response);
});
} else {
return cloudFunction(request);
}
});
};

Expand Down
8 changes: 2 additions & 6 deletions test/helpers/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,14 @@ AV.Cloud.define('testRun', function(request, response) {
);
});

AV.Cloud.define('testRun_promise', function(request, response) {
AV.Cloud.run('choice', {choice: true}).then(function(data) {
AV.Cloud.define('testRun_promise', function(request) {
return AV.Cloud.run('choice', {choice: true}).then(function(data) {
assert.equal('OK~', data);
AV.Cloud.run('choice', {choice: false}).then(function(data) {
assert.ifError(data);
}, function(err) {
assert.equal('OMG...', err);
response.success();
});
},
function(err) {
assert.ifError(err);
});
});

Expand Down