【问题标题】:why cloud funtion in node js doesnt deploy propely为什么 node js 中的云功能无法正确部署
【发布时间】:2019-02-11 14:49:35
【问题描述】:

这里我使用云函数创建用户我正在使用 express 模块,当我尝试部署此代码时,它部署到云函数并显示错误消息:函数未正确部署

const express = require('express');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceaccount = require('./ServiceAccountKey.json');

const app = express();

admin.initializeApp({
credential: admin.credential.cert(serviceaccount)
});


app.post('/',(req,res)=>{
if(req.method!== 'POST'){
    res.status(400).send("what are u trying baby");
    return;
}
admin.auth().createUser({
    email:req.body.email,
    password: req.body.pass,
}).then(function(userRecord){
    res.send({'uid': userRecord.uid});
    return;
}).catch(function(error){
    res.send({'error': 'Try COrrect one baby'});
    return;
});
return;
});
exports.register = funtions.Https.onRequest(app);

当我在结尾添加这个时

module.exports = {
app
}

它显示已部署的功能,但未显示在云功能仪表板中

我在这里做错了什么

这是我得到的错误,我知道错误是什么

⚠  functions[register(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js     can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:476:15)
at Function.Module._load (module.js:424:25)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/index.js:1:79)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)


Functions deploy had errors with the following functions:
    register

【问题讨论】:

    标签: node.js firebase google-cloud-functions


    【解决方案1】:

    您的代码从不使用firebase-functions 模块来声明云函数。您的 functions 变量将未使用。您不能只导出任何期望它运行的函数 - 它必须由 SDK 构建。

    如果你有一个 express 应用要部署,你需要通过一个 HTTP 类型的函数来导出它:

    exports.app = functions.https.onRequest(app);
    

    【讨论】:

    • 对不起,我忘了补充说我编辑了我的问题,我已经将它包含在我的代码中也是同样的问题
    • 你为什么还要直接导出app?也许您应该编辑问题并包含整个文件内容,而不是试图分解它。
    • 我不知道我应该保留那个 module.export r 不是因为我不知道它是因为我看到了一些文档并且我尝试了它是否需要
    • 我尝试将 madule.export{app} 添加到我的代码中,它已成功部署,但未显示在云功能仪表板中,当我删除它时尝试它给出了函数未正确部署但已部署的错误并在仪表板中显示并获取 url 但该错误意味着什么
    • 如果仪表板没有像您期望的那样下雪,您应该直接联系 Firebase 支持。 firebase.google.com/support/contact
    【解决方案2】:

    首先,我认为您需要更改 url 的端点。不要只放'/'。也许像'/user/create'。

    app.post('/user/create',(req,res)=>{
      if(req.method!== 'POST'){
        res.status(400).send("what are u trying baby");
        return;
      }
      admin.auth().createUser({
        email:req.body.email,
        password: req.body.pass,
      }).then(function(userRecord){
         res.send({'uid': userRecord.uid});
         return;
      }).catch(function(error){
        res.send({'error': 'Try COrrect one baby'});
        return;
      });
      return;
    });
    
    exports.register = funtions.Https.onRequest(app);
    

    并且在您的firebase.json 中您应该重写网址:

    {
      "functions": {
           ...
       },
      "hosting": {
         "public": "public",
         "rewrites": [
            { 
              "source": "/user/create",
              "function": "register" 
            }
         ]
      }
    }
    

    更多解释可以关注tutorial

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2019-04-25
      • 2020-10-15
      • 2021-11-17
      相关资源
      最近更新 更多