【问题标题】:Firebase Request simplification and error fixingFirebase 请求简化和错误修复
【发布时间】:2020-03-23 02:07:21
【问题描述】:

我有一堆表单,它们使用快速 API 和云函数将数据输入到我的 firebase 数据库中。

我有一个主要问题 - 在输入表单数据时尝试测试我的 API 时出错。

次要问题更多,有没有比我目前做的更干净的方法将输入的表单数据带到火力基地。

这是创建新工作流的代码(表单输出的数据)

(this is the required code from the route file)

const {getAllWorkflows,
    postOneWorkflow} = require('./handlers/workflow');
// Workflow Routes

app.get('/Workflows', getAllWorkflows);
app.post('/Workflow', postOneWorkflow);

这是来自处理程序文件的请求代码

exports.postOneWorkflow = (req, res) => {
  const newWorkflow = {
    completed: req.body.completed,
    currentStep: req.body.currentStep,
    createdAt: new Date().toISOString(),
    Reason: req.body.Reason,
    breach: req.body.breach,
    cauInd: req.body.cauInd,
    cauOth: req.body.cauOth,
    cauWea: req.body.cauWea,
    claimEoT: req.body.claimEoT,
    dateAware: req.body.dateAware,
    dateEoTClaim: req.body.dateEoTClaim,
    daysClaimed: req.body.daysClaimed,
    dec: req.body.dec,
    delayRespon: req.body.delayRespon,
    descB: req.body.descB,
    descCau: req.body.descCau,
    descExt: req.body.descExt,
    event: req.body.event,
    eviCause: req.body.eviCause,
    eviExtent: req.body.eviExtent,
    ifGranDay: req.body.ifGranDay,
    notice: req.body.notice,
    proMitPro: req.body.proMitPro,
    proResPro: req.body.proResPro,
    recWri: req.body.recWri,
    stepsMit: req.body.stepsMit,
    stepsPre: req.body.stepsPre

  };

  db.collection("Workflow")
    .add(newWorkflow)
    .then(doc => {
      const resWorkflow = newWorkflow;
      resWorkflow.WorkflowId = doc.id;
      res.json(resWorkflow);
    })
    .catch(err => {
      res.status(500).json({ error: "something went wrong" });
      console.error(err);
    });
};

我目前收到一个错误

SyntaxError: Unexpected token c in JSON at position 10

当使用 postman 将 json 输入到工作流 post 路由时,

{
        completed: "False", 
        currentStep: 1,
        claimEoT: "True",
        event: "True", 
        notice: "True", 
        recWri: "True", 
        dateEotClaim: "",  
        dateAware: "",  
        eviCause: "",    
        descCau : "",
        eviExtent : "True",
        descExt : "",
        daysClaimed : 5,
        delayRespon : "True",
        proResPro : 1,
        stepsPre : "True",
        proPrePro : 1,
        stepsMit: "True",
        proMitPro : 10 ,
        breach : "True",
        descB : "describe",
        cauInd : "True",
        cauWea : "True",
        cauOth : "True",
        dec : "True",
        ifGranDay : 5,
        Reason : "I AM THE SENATE"
      }

不是 100% 确定为什么,因为我的格式似乎很好?

通过创建查询 - 我想知道除了为查询编写大量代码之外是否有更有效的方法。

关键是我需要能够查询不同的集合并对其进行编辑,但我觉得查询只是接受了它被告知的内容,并将其发送到这个数据库,而无需指定具体的“完成”或“原因”,将是理想的。

【问题讨论】:

    标签: json firebase api express http


    【解决方案1】:
    1. 您的邮递员 JSON 无效。键也应该用引号括起来。
    {
     "completed": "False", 
     "currentStep": 1,
     ...
    } 
    
    1. 您可以从 req.body 创建 newWorkflow 对象!
      const newWorkflow = req.body;
      newWorkflow.createdAt = new Date().toISOString();
    

    【讨论】:

    • 该死 - 利他林是一种极好的药物 - 感谢愚蠢的错误,感谢您的建议!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多