Google Firebase Error(Function returned undefined, expected Promise or value)

后端 未结 3 1148
有刺的猬
有刺的猬 2020-12-24 05:11

I\'m developing Server with Firebase.

I had copied Google Developer\'s Video on Youtube.

It works well, but on log there is an error:

3条回答
  •  太阳男子
    2020-12-24 05:56

    Adding to what @bob-snyder said, your problem is that your returning undefined under a condition.

    if (post.sanitized) return;
    

    My suggestion is to use a single exit point, which is a common tip when programming. Article.

    Example

    // code...
    const SUCCESS_CODE = 0;
    
    exports.sanitizePost = functions.database
        .ref('/posts/{pushId}')
        .onWrite(event => {
            const post = event.data.val();
    
            let response = Promise.resolve(SUCCESS_CODE);
    
            if (!post.sanitized) {
                console.log('Sanitizing new post', event.params.pushId);
                console.log(post);
                post.sanitized = true;
                post.title = sanitize(post.title);
                post.body = sanitize(post.body);
                response = event.data.ref.set(post); 
            }
    
            return response;
        })
    

提交回复
热议问题