我有一个场景,在这个场景中,我试图确定公司当月有多少新的“付款人”。我使用的代码有效,我可以记录结果并获取公司预期的新付款方数量。但是,该方法返回0,因为最终代码是在Promise完成之前执行的。
我认为问题在于,promise不是链式的,而是嵌套的,我尝试将它们链接在一起,但是它只是给出了错误。下面的代码明显有什么问题吗?
Parse.Cloud.define('cloudMethod', function(request, response) {
if (!request.user) {
response.error('Invalid User')
return
}
// Set up now date
const now = new Date()
const thisMonthYear = now.getMonth().toString() + now.getFullYear().toString()
// Setup empty array to hold new monthly payer matches
const newMonthlyDonors = []
// Setup User query
const User = Parse.Object.extend('User')
const userQuery = new Parse.Query(User)
// Step 1: Get company pointer from user
userQuery
.equalTo('username', request.user.get('username'))
.first()
.then(function(user) {
// Step 2: Count payers for that company
var Payment = Parse.Object.extend('Payment')
var paymentQuery = new Parse.Query(Payment)
paymentQuery.equalTo('company', user.get('company'))
// Create a trivial resolved promise as a base case.
var promise = Parse.Promise.as()
paymentQuery.distinct('user').then(function(results) {
// Step 3: Loop through each distinct payer
_.each(results, function(result) {
// For each item, extend the promise with a function.
promise = promise.then(function() {
// Setup new Payment query
const firstPaymentQuery = new Parse.Query(Payment)
/*
Step 4:
Query Payment class by this user,
set payment dates in ascending order
and then take the first one.
*/
firstPaymentQuery
.equalTo('user', result)
.ascending('paymentDate')
.first()
.then(function(firstPayment) {
// Set up user date
const firstPaymentDate = new Date(firstPayment.get('paymentDate'))
const firstPaymentMonthYear = firstPaymentDate.getMonth().toString() + firstPaymentDate.getFullYear().toString()
/*
Step 5:
See if the user's first payment date is equal to the current month
*/
if (firstPaymentMonthYear === thisMonthYear) {
return newMonthlyDonors.push(result)
}
else {
return
}
}, function(error) {
response.error('Query to get users first payment date failed')
})
})
})
return promise
}).then(function() {
/*
FIXME:
This is getting called before the queries above can run.
Which is why it's returning 0...
*/
// Return the matches for this month
response.success(newMonthlyDonors.length)
}, function(error) {
response.error('total user count for company failed')
})
},
function(error) {
console.log('Error retrieving User')
console.log(error)
})
})
答案 0 :(得分:0)
我会考虑创建一个包含所有诺言const promises = [];
的数组,因此,每次有诺言时,都将其推入数组。
然后,您将返回所有这样的承诺:return Promise.all(promises);
Parse.Cloud.define('cloudMethod', function(request, response) {
...
const promises = [];
promises.push(firstPaymentQuery
.equalTo('user', result)
.ascending('paymentDate')
.first()
.then(function(firstPayment) { ...
);
...
// callback if all promises have finished
Promise.all(promises).then(() => {
// success
});
});
这是我通常要做的。
答案 1 :(得分:0)
问题是我不是因为import os
working_directory = os.path.dirname(__file__)
properties_file = os.path.join(working_directory, 'module', 'properties.yaml')
中调用的第一个promise的结果而返回查询,所以从未返回该查询中的返回结果。
引用问题中的大部分代码:
此:
_.each()
需要这样:
/*
Step 4:
Query Payment class by this user,
set payment dates in ascending order
and then take the first one.
*/
firstPaymentQuery…