【发布时间】: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