【问题标题】:Mongoose unable to save or persist to database with a POST Request; gives no errorMongoose 无法通过 POST 请求保存或持久保存到数据库;没有错误
【发布时间】:2021-02-14 03:36:27
【问题描述】:

我正在尝试通过 Postman 将数据发送到由 Node.js 和 Mongoose 组成的 REST API

我收到以下回复:

但这不会持续存在或保存到 Mongo DB:

我添加了以下内容:

mongoose.set("debug", (collectionName, method, query, doc) => {
    console.log(`${collectionName}.${method}`, JSON.stringify(query), doc);
});

所以我也在控制台上得到这个(会话应该为空)?

standups.insertOne {"_id":"5f9e54cea6d454065f0a963b","teamMember":"Mark","project":"Trinity Web Application","workYesterday":"Build out the Models","workToday":"Testing the API Endpoint using Postman","impediment":"None","createdOn":"2020-10-31T22:30:03.000Z","__v":0} { session: null }

我在我的服务器后端配置了以下文件:

api/routes/standup.js

const Standup = require('../../models/standup')

module.exports = function (router) {
    // GET: the 12 newest stand-up meeting notes
    router.get('/standup', function (req, res) {

    })

    // POST: Get new meeting note document...
    router.post('/standup', async function (req, res) {
        let note = new Standup(req.body)
        await note.save(function (err, note) {
            if (err) {
                return res.status(400).json(err)
            }
            res.status(200).json(note)
        })
    })
}

api/models/standup.js

const mongoose = require('mongoose')

const standupSchema = new mongoose.Schema({
    teamMember: { type: String },
    project: { type: String },
    workYesterday: { type: String },
    workToday: { type: String },
    impediment: { type: String },
    createdOn: { type: Date, default: Date.now }
}, { bufferCommands: false })

module.exports = mongoose.model('Standup', standupSchema)

app.js

const express = require('express')
const app = express()
const api = require('./api')
const morgan = require('morgan')
const bodyParser = require('body-parser')
const cors = require('cors')
const port = process.env.PORT || 8081

app.set('port', port)

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(cors())
app.use('/api', api)
app.use(express.static('static'))
app.use(morgan('dev'))
app.use(function (req,res) {
    const err = new Error('Not Found')
    err.status = 404
    res.json(err)
})

const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/virtualstandups', {useNewUrlParser: true, bufferCommands: false})
mongoose.set("debug", (collectionName, method, query, doc) => {
    console.log(`${collectionName}.${method}`, JSON.stringify(query), doc);
});

const db = mongoose.connection

db.on('error', console.error.bind(console, 'connection error: '))
db.once('open', function() {
    console.log('Connected to MongoDB')    
    app.listen(port, function() {
        console.log('API Server Listening on port ' +  app.get('port') + '!')
    })
})

后端确实返回原始对象;但是不会持续存在并且不会发送错误。我正在通过 WSL2 使用 MongoDB 和 Node。

【问题讨论】:

    标签: node.js mongodb express mongoose postman


    【解决方案1】:

    好的,我的问题是

    1. 我在 windows 上将 Mongo DB 作为服务安装
    2. 我在不同的时间在 WSL2/Ubuntu 上安装了 mongodb(我忘了我已经在 windows 上安装了)

    两者使用相同的端口 27107

    使用 Mongo DB Compass,我只能看到没有任何变化的 Windows MongoDB;但数据实际上被发送到 WSL2/Ubuntu MongoDB。

    解决方案:

    在 Windows 上卸载 MongoDB 或仅在一个平台上运行 MongoDB。

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 1970-01-01
      • 2019-02-14
      • 2018-11-04
      • 2019-10-05
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多