ReferenceError:未定义err <br>&nbsp;在app.post

时间:2020-05-26 14:09:38

标签: javascript express google-cloud-firestore google-cloud-functions postman

我使用的材料

情况

我确实是从邮递员发来的测试不起作用,但是(正在运行)有人知道此错误吗?我确实测试了3次超过3次,但这没有用。仅发布功能不起作用。

这是我的Visual Studio的控制台

 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.

我想要东西

我从邮递员发帖,并在火库(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);
    });
});

我的云存储区

enter image description here

**邮递员**

enter image description here

1 个答案:

答案 0 :(得分:0)

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

解决方案

1。删除数据库的Firestore并重新创建数据库

  1. Firebase部署

  2. Firebase服务

  3. 在邮递员中重新测试

  4. 工作