Javascript Google Firebase 错误(函数返回未定义、预期的 Promise 或值)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47128440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:44:22  来源:igfitidea点击:

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

javascriptnode.jsfirebasefirebase-realtime-databasegoogle-cloud-functions

提问by Hyuck-Jun Lee

I'm developing Server with Firebase.

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

I had copied Google Developer's Video on Youtube.

我在Youtube 上复制了 Google Developer's Video 。

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

它运行良好,但在日志中有一个错误:

Function returned undefined, expected Promise or value

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

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

它说函数返回undefined,但我function返回一个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); 
    })

I'm beginner of Firebase, Nodejs.

我是 Firebase、Nodejs 的初学者。

回答by Bob Snyder

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

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

if (post.sanitized) return;

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

可以通过返回一个虚拟值(例如 null、false、0)来消除警告。不使用该值。

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.

当函数使用没有值的 return 语句退出时,早期版本的 Cloud Functions 不会抱怨。这解释了为什么您会return;在链接的视频和文档中看到。Firebaser Frank van Pufeelen 对该问题的评论解释了进行更改的原因。

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

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

if (post.sanitized) return 0;

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:

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

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);
    });

回答by Samer s Salib

I was getting this same error for attempting to read a document using .get()which returns a promise.

我在尝试使用.get()返回承诺的文档读取文档时遇到了同样的错误。

I found out in the official Firebase YouTube tutorialthat to resolve this error, I needed to return the promiseline of code. Check minute 4:18in the tutorial linked video [https://youtu.be/d9GrysWH1Lc]

我在官方 Firebase YouTube 教程中发现要解决此错误,我需要返回 promise代码行。在教程链接视频 [ https://youtu.be/d9GrysWH1Lc] 中查看4:18 分钟

Also, as a side note, what lead me to the tutorial solution is when I noticed in the function logs did actually log valid values but only after the function is closed, even though the error says the result was undefined.

另外,作为旁注,引导我使用教程解决方案的是,当我在函数日志中注意到实际上记录了有效值但仅在函数关闭之后,即使错误表明结果未定义。

回答by ajorquera

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

加上@bob-snyder 所说的,您的问题是您undefined在某种条件下返回。

if (post.sanitized) return;

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

我的建议是使用单个 exit point,这是编程时的一个常见技巧。

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;
    })