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

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

问题描述

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

I'm developing Server with Firebase.

我在 Youtube

I had copied Google Developer's Video on Youtube.

效果很好,但是在日志中有错误:

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

函数返回未定义、预期的 Promise 或值

Function returned undefined, expected Promise or value

它说函数返回 undefined,但我让 function 返回一个 promise `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(/stupid/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.

推荐答案

正如 Frank 在他对您的帖子的评论中指出的那样,产生警告的 return 语句是这个:

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;文档.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天全站免登陆