【发布时间】:2019-04-24 15:18:53
【问题描述】:
我正在我的云代码中触发此功能。在我添加退货和批处理行之前,一切正常,但由于缺少退货承诺而出现错误,并且写入需要很长时间。
在关注其中一些视频后here 我尝试更正返回承诺,但现在该功能根本不起作用
我来自 swift 背景,对 javascript 不太熟悉。 我试过下面的代码
exports.updateFeeds = functions.firestore
.document('feedItems/{feedID}')
.onUpdate((change, context) => {
const newValue = change.after.data();
const oldValue = change.before.data();
const uid = newValue.uid;
var userRef = db.collection("users").doc(uid);
var authorRef = db.collection("users").doc(newValue.creatorUid);
var postRef = db.collection("posts").doc(newValue.postId);
var resharedRef = db.collection("resharedPostPopularities").doc(newValue.postId);
var feedRef = db.collection("feedItems");
let followers = [];
if (newValue.wasViewed && !oldValue.wasViewed) {
authorRef.get().then((doc) => {
let data = doc.data();
let postview_count = data.postViewCount;
return authorRef.update({
postViewCount: Number(postview_count) + 1
}).then(() => {
return userRef.collection('viewedPosts').doc('content').update({
postIds: admin.firestore.FieldValue.arrayUnion(newValue.postId)
}).then(() => {
postRef.get().then((doca) => {
let datap = doca.data();
let postresh_count = Number(datap.reshareCount);
let postview_count = Number(datap.viewCount) + 1;
if (newValue.wasReshared) {
postresh_count = postresh_count + 1;
}
let popu = (postresh_count / postview_count) || 0;
if (postview_count * postresh_count == 0) {
popu = 0;
}
return postRef.update({
viewCount: postview_count,
popularity: popu,
reshareCount: postresh_count
}).then(() => {
postRef.collection('views').doc('content').get().then((docr) => {
if (typeof docr.data() == 'undefined') {
return postRef.collection('views').doc('content').set({
uids: admin.firestore.FieldValue.arrayUnion(uid)
});
}
else {
return postRef.collection('views').doc('content').update({
uids: admin.firestore.FieldValue.arrayUnion(uid)
});
}
});
if (newValue.wasReshared) {
postRef.collection('reshares').doc('content').get().then((elon) => {
console.log('elon is');
console.log(elon.data());
if (typeof elon.data() == "undefined") {
return postRef.collection('reshares').doc('content').set({
uids: [uid]
});
}
else {
return postRef.collection('reshares').doc('content').update({
uids: admin.firestore.FieldValue.arrayUnion(uid)
});
}
});
userRef.collection('followers').doc('content').get().then((doc) => {
let data = doc.data();
if (typeof data.uids != 'undefined') {
followers = data.uids;
console.log('followers is:');
console.log(followers);
}
}).then(() => {
let rep = 0;
let validis = [];
let batch = db.batch();
followers.forEach((fol) => {
rep++;
if (fol != newValue.creatorUid && fol != uid) {
feedRef.where('postId', '==', newValue.postId).where('uid', '==', fol).get().then((snapshot) => {
if (snapshot.empty) {
validis.push(fol);
console.log('No feed with post ' + newValue.postId + ' and user ' + fol);
// here we want to add the batch for this write
batch.set(feedRef, {
createdAt: admin.firestore.FieldValue.serverTimestamp(),
uid: fol,
creatorUid: newValue.creatorUid,
postId: newValue.postId,
isResharedPost: true,
wasViewed: false,
wasReshared: false,
wasLiked: false,
wasDirectlyShared: false
})
batch.commit().then((res) => {
console.log(rep + ' out of ' + followers.length);
if (rep == followers.length) {
console.log('finally its ' + followers.length);
console.log('resharereffffingngg');
resharedRef.get().then((docr) => {
if (typeof docr.data() == 'undefined') {
console.log('docr unexists');
return resharedRef.set({
popularity: popu,
uids: validis
});
}
else {
console.log('docr exists');
let datar = docr.data();
let datar_uids = datar.uids;
let c = datar_uids.concat(validis.filter(function (item) {
return datar_uids.indexOf(item) < 0;
}));
return resharedRef.update({
popularity: popu,
uids: c
});
}
});
}
});
}
});
}
});
});
}
resharedRef.get().then((docr) => {
console.log(docr.data());
console.log(typeof docr.data());
if (typeof docr.data() != 'undefined') {
return resharedRef.update({
uids: admin.firestore.FieldValue.arrayRemove(newValue.uid)
});
}
});
});
});
});
});
});
}
});
【问题讨论】:
标签: javascript firebase google-cloud-firestore google-cloud-functions