I just started to learn express recently. In the practice of tapping mdn, I want to modify it a little bit. If I encounter a problem, I will first add the code:
//tagController.js:
const Tag = require("../models/tag");
exports.find_all = async (req, res, next) => {
let result = await Tag.find({});
debugger;
res.send(result);
};
///./models/tag
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
var TagSchema = new Schema({
context: String,
tagId: ObjectId
//tagId: {type: ObjectId, ref: "Tag", required: true},
});
TagSchema
.virtual("url")
.get(function () {
return "/tag/" + this._id;
});
module.exports = mongoose.model("Tag", TagSchema, "Tag");
//routes.js
const express = require("express");
const router = express.Router();
const tag_controller = require("../controllers/tagController");
router.get("/", tag_controller.find_all);
//Set up mongoose connection
var mongoose = require("mongoose");
var mongoDB = "mongodb://****************************/test?ssl=true&replicaSet=piggybank-shard-0&authSource=admin&retryWrites=true";
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
autoIndex: false, // Don"t build indexes
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0,
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
// replicaSet: "piggybank-shard-0",
// ssl: true,
// authSource: admin,
// retryWrites: true
};
mongoose.connect(mongoDB,options);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
Database connection should be normal. Data can be inserted into the table. If there is a problem with the query, I would like to ask where it may be wrong. Thank you:)