云功能:错误:无文档可更新

时间:2019-07-25 08:29:55

标签: node.js typescript firebase google-cloud-firestore google-cloud-functions

我的数据库已建立,因此每当用户发布帖子时,其名称和图像信息就会与该帖子一起保存,以保存阅读(通过不要求对正在阅读的每个帖子都要求对用户数据进行读取)。 / p>

我正在尝试构建一个函数,当该用户在设置中更改其姓名时,该用户的所有帖子实例中该用户的姓名都将更改。

*注意:我应用中的所有帖子都是问题。

我已经部署了它并尝试对其进行测试,通过更改用户名,我可以肯定有一个问题,即他们是谁的作者(我可以在数据中看到这一点)。只要我正确使用通配符,该路径也是正确的,据我所知。

当我这样做时,他们的名字在问题数据集中不会改变。

当我查看日志时,看到此错误:

Error: No document to update: projects/my-projects-name/databases/(default)/documents/communities_data/4ORj0bh9rpi2Ns5vj4pW/questions/undefined
    at Http2CallStream.call.on (/srv/node_modules/@grpc/grpc-js/build/src/client.js:96:45)
    at emitOne (events.js:121:20)
    at Http2CallStream.emit (events.js:211:7)
    at process.nextTick (/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22)
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

我不知道为什么一定会出现一个与该查询相对应的文档。

这是我的功能:

exports.updateUser = functions.firestore.document('communities_data/{community}/profiles/{profileID}').onUpdate((change, context) => {
    // Retrieve the current and previous value
    const data = change.after.data();
    const previousData = change.before.data();

    // We'll only update if the name has changed.
    // This is crucial to prevent infinite loops.
    if (data !== undefined && previousData !== undefined) {

        if (data.name !== previousData.name || data.image !== previousData.image) {

            return db.collection('communities_data/' + context.params.community + '/questions').where('author_ID', '==', context.params.profileID).get()
                .then(snapshot => {

                    if(snapshot.size >0){
                        snapshot.forEach(doc => {
                            const question = doc.data as unknown as Question
                            const promises = []
                            const p = db.doc('communities_data/' + context.params.community + '/questions/' + question.id)
                                .update({
                                    author_name: data.name,
                                    author_image: data.image
                                });
                            promises.push(p)
                            return Promise.all(promises)
                        })
                    }
                }
                )

        } else {
            return null;
        }
    } else {
        return null;
    }
});

这是我的Question类:

class Question {
    id: string;
    title: string;
    details: string;
    language: Number;
    tags: Array<string>;
    timestamp: Number;
    author_ID: string;
    author_name: string;
    author_image: string;
    answers: Number;
    last_interaction: Number;
    score_items: Number;
    constructor(id: string, title: string, details: string, language: Number, tags: [], timestamp: Number, author_ID: string, author_name: string, author_image: string, answers: Number, last_interaction: Number, score_items: Number) {
        this.id = id;
        this.title = title;
        this.details = details;
        this.language = language;
        this.tags = tags;
        this.timestamp = timestamp;
        this.author_ID = author_ID;
        this.author_name = author_name;
        this.author_image = author_image;
        this.answers = answers;
        this.last_interaction = last_interaction;
        this.score_items = score_items;
    }
}

0 个答案:

没有答案