首页 > 解决方案 > ReferenceError: err 未
   在 app.post中定义

问题描述

我使用的材料

情况

我确实从邮递员那里发布了无法正常工作的测试但是(得到工作)有人知道这个错误吗?我做了 3 次以上的测试,但那没有用。只有发布功能没有功能。

这是我的视觉工作室的控制台

 functions: Finished "api" in ~1s
>  (node:28624) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
>      at ServerResponse.setHeader (_http_outgoing.js:470:11)
>      at ServerResponse.header (C:\Users\taiga\Github\React2\Full-stack-app\socialape-functions\functions\node_modules\express\lib\response.js:771:10)       
>      at ServerResponse.send (C:\Users\taiga\Github\React2\Full-stack-app\socialape-functions\functions\node_modules\express\lib\response.js:170:12)
>      at ServerResponse.json (C:\Users\taiga\Github\React2\Full-stack-app\socialape-functions\functions\node_modules\express\lib\response.js:267:15)
>      at admin.firestore.collection.add.then.catch (C:\Users\taiga\Github\React2\Full-stack-app\socialape-functions\functions\index.js:61:23)
>      at process._tickCallback (internal/process/next_tick.js:68:7)
>  (node:28624) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a 
catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
>  (node:28624) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.

我想要那个东西

我从邮递员那里发帖并在 fire-store(firebase) 中创建数据

index.js

const functions = require('firebase-functions');

const admin = require('firebase-admin');


// i added later
admin.initializeApp({ credential: admin.credential.applicationDefault() });

// admin.initializeApp(functions.config().firebase);


const express = require('express');

const app = express();

// i added later
const firestore  = admin.firestore();
firestore.settings({ignoreUndefinedProperties:true});


app.get('/screams', (req, res) => {

  admin
  .firestore()
  .collection('screams')
  .get()
    .then(data => {
      let screams = [];
      data.forEach( (doc) =>  {
        screams.push(doc.data());
      });
      return res.json(screams);
    })
    .catch((err) => console.error(err));
});

// https://http://taiga.../api/


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




app.post('/scream',(req, res) => {

   const newScream = {
    body: req.body,
    userHandle: req.userHandle,
    createdAt: admin.firestore.Timestamp.fromDate(new Date())
   };

   admin
   .firestore()
   .collection('screams')
   .add(newScream)
   .then( (doc) => {
     res.json({ message:  `document ${doc.id} created successfully`});
   })
   .catch((err) => {
      res.status(500).json({ error: 'something went wrong'});
      console.error(err);
    });
});

我的云火库

在此处输入图像描述

**邮递员**

在此处输入图像描述

标签: javascriptexpressgoogle-cloud-firestoregoogle-cloud-functionspostman

解决方案


我本可以解决这个问题,但我不知道为什么

解决方案

1.删​​除了数据库的firestore并重新创建了数据库

  1. Firebase 部署

  2. Firebase 服务

  3. 在邮递员中重新测试

  4. 在职的


推荐阅读