Google Firebase错误(函数返回未定义,预期的Promise或值) [英] Google Firebase Error(Function returned undefined, expected Promise or value)

查看:106
本文介绍了Google Firebase错误(函数返回未定义,预期的Promise或值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase开发服务器。

I'm developing Server with Firebase.

我在 Youtube

它运作良好,但在日志中有错误:

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


函数返回未定义,预期Promise或value

Function returned undefined, expected Promise or value

它说函数返回 undefined ,但我使函数返回承诺`set``

It says function returned undefined, but I make function return a promise `set``

我该怎样解决这个问题?

How can I solve this?

function sanitize(s) {
    var sanitizedText = s;
    console.log('sanitize params: ', sanitizedText);
    sanitizedText = sanitizedText.replace(/\bstupid\b/ig, "wonderful");
    return sanitizedText;
}
exports.sanitizePost = functions.database
    .ref('/posts/{pushId}')
    .onWrite(event => {
        const post = event.data.val();
        if (post.sanitized) return;

        console.log('Sanitizing new post', event.params.pushId);
        console.log(post);
        post.sanitized = true;
        post.title = sanitize(post.title);
        post.body = sanitize(post.body);
        return event.data.ref.set(post); 
    })

我'是Firebase的初学者,Nodejs。

I'm beginner of Firebase, Nodejs.

推荐答案

正如弗兰克在对你的帖子的评论中指出的那样,产生警告的返回语句是这一个:

As Frank indicates in his comment on your post, the return statement that is producing the warning is this one:

if (post.sanitized) return;

通过返回虚拟值(例如null,false,0)可以使警告静音。该值未被使用。

The warning can be silenced by returning a dummy value (e.g. null, false, 0). The value is not used.

当使用没有值的return语句退出函数时,早期版本的Cloud Functions没有抱怨。这就解释了为什么您在链接的视频中以及 return; rel =noreferrer>文档。 Firebaser Frank van Pufeelen对此问题的评论解释了为何进行了更改。

Earlier versions of Cloud Functions did not complain when a function exited using a return statement with no value. That explains why you see return; in the video you linked and in the documentation. The comment on the question by Firebaser Frank van Pufeelen, explains why the change was made.

消除警告的最简单方法是添加返回值,正如Frank建议的那样:

The simplest way to eliminate the warning is to add a return value, as Frank suggested:

if (post.sanitized) return 0;

另一种选择是从更改触发器onWrite() onCreate()。然后在清理帖子时不会调用该函数,并且不需要产生警告的检查:

Another option is to change the trigger from onWrite() to onCreate(). Then the function will not be invoked when the post is sanitized and the check that produces the warning is not needed:

exports.sanitizePost = functions.database
    .ref('/test/{pushId}')
    .onCreate(event => {  // <= changed from onWrite()
        const post = event.data.val();
        //if (post.sanitized) return; // <= no longer needed

        console.log('Sanitizing new post', event.params.pushId);
        console.log(post);
        //post.sanitized = true; // <= not needed when trigger is onCreate()
        post.title = sanitize(post.title);
        post.body = sanitize(post.body);
        return event.data.ref.set(post);
    });

这篇关于Google Firebase错误(函数返回未定义,预期的Promise或值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆