gpt4 book ai didi

node.js - Mongodb/Node.js findByIdAndUpdate 方法未更新

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:42 26 4
gpt4 key购买 nike

我正在尝试使用 findByIdAndUpdate 方法更新 mongo 数据库中的集合。我点击端点并发送包含更新信息的请求,响应始终是更新之前的信息,并且更新永远不会出现在我的数据库中。

我做错了什么?

下面是我的 edit.js 文件:

const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const dbConfig = require('../mongo/dbConfig');
const Band = require('../mongo/models/band');
const mongoose = require('mongoose');


// Initialize the router
const router = express.Router();
mongoose.connect(dbConfig.devUrl);

//init body parser
router.use(
bodyParser.urlencoded({
extended: true
})
);

router.use(bodyParser.json());

router.post('/band/editProfile', function (req, res) {
console.log(req.body);
console.log("it hit !!!!!!!!!!!!!!!!!!");
Band.findByIdAndUpdate(
// the id of the item to find

req.body.id,
{
bands: {
bandName: req.body.bandName,
genre: req.body.genre,
numberOfMembers: req.body.numberOfMembers,
city: req.body.city,
state: req.body.state,
zip: req.body.zip,
travel: req.body.travel,
travelDistance: req.body.travelDistance,
spotify: req.body.spotify,
youtube: req.body.youtube,
soundcloud: req.body.soundcloud,
twitter: req.body.twitter,
facebook: req.body.facebook
}
},
function (err, response) {
// Handle any possible database errors
if (err) {
console.log("we hit an error" + err);
res.json({
message: 'Database Update Failure'
});
}
console.log("This is the Response: " + response);
}
);
});

这是我的乐队模型:

//Module Imports
const mongoose = require('mongoose');

//Initializes a mongoose schema to be used with the document
const bandSchema = new mongoose.Schema({
bandName: String,
genre: String,
numberOfMembers: Number,
city: String,
state: String,
zip: String,
travel: Boolean,
travelDistance: Number,
spotify: String,
youtube: String,
soundcloud: String,
twitter: String,
facebook: String
});

//Creates the Schema to be used with the export
let Band = mongoose.model('band', bandSchema);

//Exports the Schema to the API
module.exports = Band;

我已经对此进行了相当彻底的研究,但遇到了困难,因此非常感谢您的帮助!

最佳答案

bands 位于您的更新对象中,但不在您的架构中。只需删除更新对象的额外层,更新就应该可以工作。但您还需要包含 {new: true} 选项作为单独的参数,以便返回更新的文档而不是原始文档(请参阅 docs )。

Band.findByIdAndUpdate(
req.body.id,
{
bandName: req.body.bandName,
genre: req.body.genre,
numberOfMembers: req.body.numberOfMembers,
city: req.body.city,
state: req.body.state,
zip: req.body.zip,
travel: req.body.travel,
travelDistance: req.body.travelDistance,
spotify: req.body.spotify,
youtube: req.body.youtube,
soundcloud: req.body.soundcloud,
twitter: req.body.twitter,
facebook: req.body.facebook
},
{new: true},
function (err, response) {
// Handle any possible database errors
if (err) {
console.log("we hit an error" + err);
res.json({
message: 'Database Update Failure'
});
}
console.log("This is the Response: " + response);
}
);

关于node.js - Mongodb/Node.js findByIdAndUpdate 方法未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49723084/

26 4 0
文章推荐: c - 我在结束 C 中的循环时遇到问题
文章推荐: css - 图像叠加层不起作用
文章推荐: c - 我想用C语言将两个变量初始化为一个二维数组
文章推荐: html - 为什么 Twitter 的固定位置导航栏使用这么多
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com