Javascript 在邮递员上发布格式化字符串

Javascript 在邮递员上发布格式化字符串,javascript,node.js,reactjs,express,postman,Javascript,Node.js,Reactjs,Express,Postman,我有一个网站,让人们回答编码问题。我想把我问他们的问题保存在mongodb数据库中,以及他们发送的答案。在测试我在express应用程序中设置的路由时,我无法确定如何在请求中发送此格式化文本以成功将其保存到数据库。我希望保留这种格式,因为我在react前端使用了几个npm包来解析这些数据,以生成代码片段 这是我的猫鼬模型: import mongoose from 'mongoose'; const codingProblemSchema = mongoose.Schema( {

我有一个网站,让人们回答编码问题。我想把我问他们的问题保存在mongodb数据库中,以及他们发送的答案。在测试我在express应用程序中设置的路由时,我无法确定如何在请求中发送此格式化文本以成功将其保存到数据库。我希望保留这种格式,因为我在react前端使用了几个npm包来解析这些数据,以生成代码片段

这是我的猫鼬模型:

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]`,
  };
以下是我的邮递员发帖请求失败的屏幕截图:


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

JSON只支持双引号,不能用单引号或字符串文字将属性和值括起来

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

     JSON.parse(JSON.stringify(codeProblem ))

这就解决了这个问题。但是实际的代码部分需要保持函数的间距。
     JSON.parse(JSON.stringify(codeProblem ))