【发布时间】:2019-09-17 08:05:34
【问题描述】:
我用 Mongoose 在 nodejs express 上做了一个路由器。
当我通过邮递员发送const { clientID, orderID } = req.body; 时,
await matching.save(); 运行良好。
但之后它会出现如下错误。
我该如何解决这个问题?
TypeError:matching.post 不是函数 在 router.post (D:\Dev\matching\matching\routes\match ing.js:24:20) 在 process._tickCallback (internal/process/next_tick.js:68:7)
var express = require("express");
var router = express.Router();
const Matching = require("./../models/matching");
function wait(time) {
return new Promise(resolve => {
setTimeout(resolve, time);
});
}
router.post("/orderpost", async (req, res, next) => {
try {
const { clientID, orderID } = req.body;
const listOn = true;
const matching = new Matching({
clientID,
orderID,
listOn
});
await matching.save();
await matching.post("save", (doc, next) => {
wait(3000).then(() => {
console.log("good");
next();
});
});
res.send("good");
} catch (err) {
console.error(err);
}
});
我还添加了 Schema 模型。
const mongoose = require("mongoose");
const { Schema } = mongoose;
const MatchingSchema = new Schema({
clientID: String,
riderID: [String],
orderID: String,
listOn: Boolean
});
module.exports = mongoose.model("Matching", MatchingSchema);
【问题讨论】:
-
你的
Matching是什么,是Schema还是Model,貌似是后者,能不能也把代码贴在models/matching@ -
我附上了,非常感谢您的关注!