ESLint error trying to deploy functions Firebase

大城市里の小女人 提交于 2019-11-29 04:29:17

The ESLint warnings are legitimate. To get more detail on each one, you can search them using their unique id. For example:

15:3   error  Expected catch() or return                  promise/catch-or-return

The id of the warning here is promise/catch-or-return. Googling for "eslint promise/catch-or-return" finds a plugin for ESLint that describes the warning as:

Enforces the use of catch() on un-returned promises

You're getting this because you have a promise being returned from then, but you never return or catch for possible errors.

The second warning:

15:69  error  Each then() should return a value or throw  promise/always-return

comes from the same ESLint plugin, and is described like this:

Return inside each then() to create readable and reusable Promise chains.

It's requiring you to return a value inside each function called by then, even if you don't have anything special to sent to the next promise chain.

You can resolve both of these problems like this:

admin.database().ref('/messages').push({original: original}).then(snapshot => {
    res.redirect(303, snapshot.ref);
    return null;
}).catch(error => {
    console.error(error);
    res.error(500);
});

Notice that the then block returns null now, and catch is used on the promise returned from then to capture errors and send an error response to the client.

Doug Stevenson answer is partially right as there is one flaw in it.

It should be :

admin.database().ref('/messages').push({original: original}).then(snapshot => {
    res.redirect(303, snapshot.ref);
    return 1; // IT SHOULD RETURN NON-NULL VALUE
}).catch(error => {
    console.error(error);
    res.error(500);
});

The cloud function went into infinite loop when it returned null. And as a result, we can see function timed out after 60,000 ms in the above image.

After such multiple timed out functions, you will get the error functions can be executed (shown in above pic). This is because in Spark Plan (free tier plan), there is limit on CPUMilliSecondsDailyNonbillable .

If you click on Stackdriver's Quota Policy in cloud functions section, you can see the limit in GCD( Google Cloud Platform)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!