gpt4 book ai didi

javascript - 真的很困惑 re : how to use Parse Promises correctly in Cloud Code

转载 作者:行者123 更新时间:2023-11-30 16:41:16 25 4
gpt4 key购买 nike

我不想在成功完成处理程序中嵌套各种函数,而是想将我的函数分开,以便它们可以独立使用或连续使用。但是,我对 Parse 中的 promise 语法非常困惑,尽管查看了许多其他 StackOverflow 答案(例如 herehereherehere ),但仍无法将它们成功地链接在一起。 )和Parse的官方documentation , overview , 和 blog post关于这个话题。

这是我的云代码示例,我在其中尝试使用链式 promise :

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
var account = request.object;
if (account.existed()) {
if (account.dirtyKeys().indexOf("username") >= 0) {
updateUsernameForAccountId(account.get("username"), account.id)
.then(
function() {
console.log("Checkpoint SUCCESS");
response.success();
}, function(error) {
console.log("Checkpoint ERROR");
response.error(error);
});
}
}
});

function updateUsernameForAccountId(username, accountId) {
console.log("Checkpoint A-1 ("+username+", "+accountId+")");
updateUsernameForMessagesSentByAccountId(username, accountId)
.then(
function() {
console.log("Checkpoint A-2");
return updateUsernameForMessagesReceivedByAccountId(username, accountId);
})
.then(
function() {
console.log("Checkpoint A-3");
return Parse.Promise.as();
}, function(error) {
console.log("Checkpoint A-ERROR");
return Parse.Promise.error(error);
});
};

function updateUsernameForMessagesSentByAccountId(username, accountId) {
console.log("Checkpoint B-1");
var query = new Parse.Query(parseClassMessage);
query.equalTo(parseKeySenderId, accountId);
query.find()
.then(
function(results) {
console.log("Checkpoint B-2");
var object;
for (var i = 0; i < results.length; i++) {
console.log("Checkpoint B-2-"+i);
object = results[i];
object.set(parseKeySenderUsername, username);
}
return Parse.Promise.as(results);
})
.then(
function(objects) {
console.log("Checkpoint B-3");
return Parse.Object.saveAll(objects);
})
.then(
function() {
console.log("Checkpoint B-4");
return Parse.Promise.as();
}, function(error) {
console.log("Checkpoint B-ERROR");
return Parse.Promise.error(error);
});
};

function updateUsernameForMessagesReceivedByAccountId(username, accountId) {
console.log("Checkpoint C-1");
var query = new Parse.Query(parseClassMessage);
query.equalTo(parseKeyRecipientId, accountId);
query.find()
.then(
function(results) {
console.log("Checkpoint C-2");
var object;
for (var i = 0; i < results.length; i++) {
console.log("Checkpoint C-2-"+i);
object = results[i];
object.set(parseKeyRecipientUsername, username);
}
return Parse.Promise.as(results);
})
.then(
function(objects) {
console.log("Checkpoint C-3");
return Parse.Object.saveAll(objects);
})
.then(
function() {
console.log("Checkpoint C-4");
return Parse.Promise.as();
}, function(error) {
console.log("Checkpoint C-ERROR");
return Parse.Promise.error(error);
});
};

预期的行为是:

  • 在保存用户对象之前,检查它是否已经存在(相对于新创建的用户)以及用户名是否已更改。
  • 如果是:执行函数 updateUsernameForAccountId(username, accountId),它执行 updateUsernameForMessagesSentByAccountId(username, accountId) 然后是 updateUsernameForMessagesReceivedByAccountId(username, accountId)
    • 注意我不需要这两个方法同步,但我认为有必要将单个 Promise 对象返回给 beforeSave

我能够将此代码成功上传到 Cloud Code,但是当我进入 Parse.com 上的数据浏览器并更改现有用户对象的用户名以进行测试时,日志会产生以下输出:

E2015-08-11T21:16:23.467Z]v27 before_save triggered for _User as master:
Input: {"original":{"createdAt":"2015-08-09T16:53:47.194Z","objectId":"mkTluyGbHf","updatedAt":"2015-08-11T17:23:19.523Z","username":"oldname@example.com"},"update":{"username":"newname@example.com"}}
Result: TypeError: Cannot call method 'then' of undefined
at updateUsernameForAccountId (main.js:114:4)
at main.js:16:4
I2015-08-11T21:16:23.533Z]Checkpoint A-1 (newname@example.com, mkTluyGbHf)
I2015-08-11T21:16:23.534Z]Checkpoint B-1

而且我还在 Parse.com 数据浏览器中收到以下错误:

Error: TypeError: Cannot call method 'then' of undefined at updateUsernameForAccountId (main.js:114:4) at main.js:16:4

请让我知道我可以提供哪些额外信息或说明。我提前道歉,我无法更清楚地表达我的问题、错误或困惑。

最佳答案

我自己解决了!!

问题是,正如错误明确指出的那样,我的方法 updateUsernameForAccountId 没有返回 Promise 对象。但是,问题出在 updateUsernameForMessagesSentByAccountIdupdateUsernameForMessagesReceivedByAccountId 中,因为执行 query.find() 是一种异步方法,因此不会返回任何内容它的完成处理程序立即。

为了让函数返回一个 Promise...您必须返回一个 Promise。

所以答案是确保返回query.find()

关于javascript - 真的很困惑 re : how to use Parse Promises correctly in Cloud Code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31952170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com