我是一个刚刚开始使用Parse进行休闲手机游戏的新手。
我正在编写一个云代码,客户可以通过该代码请求购买卡座。 由于代码对于解释我的问题非常重要,因此我将问题移到了代码下面。
我有以下课程:
1. User :
coin(Number)
2. Deck :
priceCoin(Number)
nickname(String)
3. User_Deck :
userId(Pointer to User)
deckId(Pointer to Deck)
类和客户请求的数据:
这就是云功能:
Parse.Cloud.define("req_purchase_deck", function(request, response) {
var purchaseDeck;
var deckQuery = new Parse.Query("Deck");
deckQuery.get( request.params.deck, function(result) {
purchaseDeck = result; // Q2 : is this valid in the scope of this function?
var userHasQuery = new Parse.Query("User_Deck");
userHasQuery.equalTo("userId", request.user);
userHasQuery.equalTo("deckId", result);
return userHasQuery.find();
}).then( function( results ) { // Q1 : Why results undefined?
if( results == null || results.length == 0 ) {
var userCoin = request.user.get("coin");
var priceCoin = purchaseDeck.get("priceCoin");
if( userCoin >= priceCoin ) {
console.log("/purchase made/" + userCoin + " - " + priceCoin + " = " + userCoin - priceCoin);
request.user.set("coin", userCoin - priceCoin);
return request.user.save();
}
else {
return Parse.Promise.error("not enough coins");
}
}
else {
return Parse.Promise.error("already has deck");
}
}).then( function(result) {
var newUserDeckObject = new Parse.Object("User_Deck");
newUserDeckObject.set("userId", request.user);
newUserDeckObject.set("deckId", purchaseDeck);
console.log("/purchase deck added");
return newUserDeckObject.save();
}).then( function(result) {
var returns = {};
returns["userCoins"] = request.user.get("coin");
returns["purchasedDeck"] = purchaseDeck.get("nickname");
response.success(returns);
console.log(request.user.username + "/purchase deck succeeded--/");
}, function(error) {
response.error(error);
});
});
结果: 已经有了deck //即使User_Deck类中根本没有行。我发现“结果”未定义。
问题1:deckQuery制作精良,但userDeckQuery导致未定义的结果:我的代码出了什么问题? 我放了很多日志来弄清楚原因,但这是我找到的唯一的东西。
问题2:我正在将deckQuery结果缓存到purchaseDeck,并且该对象在该云函数结束时是否有效?
提前致谢!
答案 0 :(得分:0)
我意识到了原因 我为我的查询使用了回调样式返回,并期望获得promise样式的结果。 我标有评论
Parse.Cloud.define("req_purchase_deck", function(request, response) {
var purchaseDeck;
var deckQuery = new Parse.Query("Deck");
//deckQuery.get( request.params.deck, function(result) {
deckQuery.get( request.params.deck ).then( function(result) { // Q1 solved.
purchaseDeck = result; // Q2 : @Fosco answered that it's fine.
var userHasQuery = new Parse.Query("User_Deck");
userHasQuery.equalTo("userId", request.user);
userHasQuery.equalTo("deckId", result);
return userHasQuery.find();
}).then( function( results ) {
if( /*results == null ||*/results.length == 0 ) { // results never be null
var userCoin = request.user.get("coin");
var priceCoin = purchaseDeck.get("priceCoin");
if( userCoin >= priceCoin ) {
console.log("/purchase made/" + userCoin + " - " + priceCoin + " = " + userCoin - priceCoin);
request.user.set("coin", userCoin - priceCoin);
return request.user.save();
}
else {
return Parse.Promise.error("not enough coins");
}
}
else {
return Parse.Promise.error("already has deck");
}
}).then( function(result) {
var newUserDeckObject = new Parse.Object("User_Deck");
newUserDeckObject.set("userId", request.user);
newUserDeckObject.set("deckId", purchaseDeck);
console.log("/purchase deck added");
return newUserDeckObject.save();
}).then( function(result) {
var returns = {};
returns["userCoins"] = request.user.get("coin");
returns["purchasedDeck"] = purchaseDeck.get("nickname");
response.success(returns);
console.log(request.user.username + "/purchase deck succeeded--/");
}, function(error) {
response.error(error);
});
});
现在的结果是"没有足够的硬币"