我正在处理一个人项目,该项目要求将Redux表单中的数据通过axios调用发送到Express服务器。我已经使用body-parser将数据从客户端传输到服务器,但是在使用mongoose保存到MongoDB时遇到了问题。为什么在我的帖子通话中收到待处理的请求?
<code>
// Client side axios post call
export const createSchedule = formValues => async (dispatch, getState) => {
const res = await axios.post("/schedule/create", {
data: {
userId: getState().auth.googleId,
title: formValues.title,
description: formValues.description
}
});
dispatch({ type: CREATE_SCHEDULE, payload: res });
};
</code>
<code>
// server side axios post call
module.exports = app => {
app.post("/schedule/create", async (req, res) => {
const schedule = new Schedule({
googleId: req.body.data.userId,
title: req.body.data.title,
description: req.body.data.description
}).save(function(err) {
if (err) console.log("saved failed.");
else console.log("saved");
});
done(null, schedule);
});
};
</code>
<code>
// Schedule schema for mongoose
const mongoose = require("mongoose");
const { Schema } = mongoose;
const scheduleSchema = new Schema(
{
googleId: String,
title: String,
description: String,
date: { type: Date, default: Date.now }
},
{ collection: "schedules" }
);
mongoose.model("schedules", scheduleSchema);
</code>
在客户端控制台中待处理结果
TypeError:Schedule不是服务器控制台中的构造函数错误。
答案 0 :(得分:0)
通过更改我在api调用中访问Schedule模型的方式来解决问题。我以前是将其导入本地的,而不是使用应该使用的mongoose.model(“ schedules”)行。