【问题标题】:Posting formatted string on postman在邮递员上发布格式化字符串
【发布时间】:2021-07-18 05:40:42
【问题描述】:

我有一个网站可以让人们回答编码问题。我想将我问他们的问题保存在 mongodb 数据库中,以及他们发送的答案。在测试我在我的快速应用程序中设置的路线时,我无法弄清楚如何在请求中发送此格式化文本以成功将其保存到数据库中。我想保持这种格式,因为我在我的 react 前端使用了一些 npm 包来解析这些数据以生成代码 sn-ps。

这是我的猫鼬模型:

import mongoose from 'mongoose';

const codingProblemSchema = mongoose.Schema(
  {
    date: {
      type: Date,
      default: Date.now(),
    },
    question: {
      type: String,
      default: true,
    },
    problem: {
      type: String,
      default: true,
    },
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

codingProblemSchema.virtual('answers', {
  ref: 'CodingAnswer',
  foreignField: 'codingProblem',
  localField: '_id',
});

const CodingProblem = mongoose.model('CodingProblem', codingProblemSchema);

export default CodingProblem;

这是我尝试发送到后端以创建新编码问题的数据示例:

const codeProblem = {
    date: 'April 15th, 2021',
    question: `You are writing the logic for a vending machine to return change to
  its customers. This function, called <code>makeChange</code>, takes
  two parameters (<code>price</code> and <code>payment</code>). Based on
  the <code>price</code> and <code>payment</code>, return how many
  dollars, quarters, dimes, nickels, and pennies the customer will
  receive back.`,
    problem: `function makeChange(price, payment) { 
    // your logic here
}

    makeChange(1, 5) // expected output -> [4, 0, 0, 0, 0]
    // price was $1 and customer paid $5. Change back is [4 dollars, 0 quarters, 0 dimes, 0 nickels, 0 pennies]
    
    makeChange(1.55, 20) // expected output -> [18, 1, 2, 0, 0]
    // price was $1.55 and customer paid $20. Change back is [18 dollars, 1 quarter, 2 dimes, 0 nickels, 0 pennies]
    
    makeChange(3.67, 100) // expected output -> [96, 1, 0, 1, 3]
    // price was $3.67 and customer paid $100. Change back is [96 dollars, 1 quarter, 0 dimes, 1 nickels, 3 pennies]`,
  };

这是我的邮递员 POST 请求失败的屏幕截图:

【问题讨论】:

    标签: javascript node.js reactjs express postman


    【解决方案1】:
    {
      "date": "April 15th, 2021",
      "question": "You are writing the logic for a vending machine to return change to\n  its customers. This function, called <code>makeChange</code>, takes\n  two parameters (<code>price</code> and <code>payment</code>). Based on\n  the <code>price</code> and <code>payment</code>, return how many\n  dollars, quarters, dimes, nickels, and pennies the customer will\n  receive back.",
      "problem": "function makeChange(price, payment) { \n    // your logic here\n}\n\n    makeChange(1, 5) // expected output -> [4, 0, 0, 0, 0]\n    // price was $1 and customer paid $5. Change back is [4 dollars, 0 quarters, 0 dimes, 0 nickels, 0 pennies]\n    \n    makeChange(1.55, 20) // expected output -> [18, 1, 2, 0, 0]\n    // price was $1.55 and customer paid $20. Change back is [18 dollars, 1 quarter, 2 dimes, 0 nickels, 0 pennies]\n    \n    makeChange(3.67, 100) // expected output -> [96, 1, 0, 1, 3]\n    // price was $3.67 and customer paid $100. Change back is [96 dollars, 1 quarter, 0 dimes, 1 nickels, 3 pennies]"
    }
    

    JSON 仅支持双引号,不能用单引号或字符串字面量括起来属性和值

    否则你可以对已有的内容进行字符串化和解析:

         JSON.parse(JSON.stringify(codeProblem ))
    

    【讨论】:

      【解决方案2】:

      只需删除邮递员中的新行,您的问题就会得到解决。我附上下面的图片。在那里选择行和 JSON 类型。

      【讨论】:

      • 这适用于这个问题。但实际代码部分我需要保持函数的间距。
      猜你喜欢
      • 1970-01-01
      • 2022-10-18
      • 2016-01-23
      • 2021-11-26
      • 2016-06-30
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多