javascript - .save 不是函数,并且 .findOneAndUpdate() 不会更新我的数据库
问题描述
我正在设置一个后端服务器,我想在路由器中做一个 put 方法。我在后端使用猫鼬快递。
当我尝试使用 .save() 更新数据库中的某些数据时,出现错误:
events.js:174 抛出错误;// 未处理的“错误”事件 ^
TypeError:PC.save 不是函数
我正在尝试使用 .findOneAndUpdate() 进行另一种解决方案,它是成功的,但它不会更新我的数据库。
const express = require('express')
const routes = express()
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", function(callback){
console.log("Connection Succeeded");
});
var PC = require("../models/PC");
//...here is my get delete etc..
这是我使用 findOneAndUpdate 的第一个解决方案
routes.put('/:id', (req, res) => {
mongoose.set('useFindAndModify', false);
PC.findOneAndUpdate(
{ 'title': req.body.title },
{ '$set': {'description': req.body.description} },
{'new': true },
function(err, PC) {
if (err) {
console.log('ERROR WHILE PUT PC');
throw (err);
} else {
console.log('Succes set');
res.status(200).send(PC)
}
}
);
})
这是我的第二个解决方案
routes.put('/:id', (req, res) => {
PC.findById(req.params.id, 'title description', function (error, pc) {
if (error) { console.error(error); }
PC.title = req.body.title
PC.description = req.body.description
console.log(PC);
PC.save(function (error) {
if (error) {
console.log(error)
}
res.send({
success: true,
message: 'PC saved successfully!',
PC: req.body
})
})
})
})
module.exports = routes;
我的模型:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var PCSchema = new Schema({
id: Number,
brand: String,
cpu: String,
memory: Number,
type: String,
vga: String,
score: Number,
title: String,
description: String
});
var PC = mongoose.model("PC", PCSchema);
module.exports = PC;
解决方案
在您的第一个示例中,您似乎发现了错误的参数,您应该使用id
.
尝试findByIdAndUpdate
改用:
routes.put('/:id', (req, res) => {
mongoose.set('useFindAndModify', false);
PC.findByIdAndUpdate(
req.params.id,
{ '$set': {'description': req.body.description, 'title': req.body.title} },
{'new': true },
function(err, pc) {
if (err) {
console.log('ERROR WHILE PUT PC');
throw (err);
} else {
console.log('Succes set');
res.status(200).send(pc)
}
}
);
})
在您的第二个示例中,您应该调用.save
结果,而不是原始PC Model
. 您可以将其更改为:
routes.put('/:id', (req, res) => {
PC.findById(req.params.id, 'title description', function (error, pc) {
if (error) { console.error(error); }
// Use lowercase `pc` on all these lines
pc.title = req.body.title
pc.description = req.body.description
console.log(pc);
pc.save(function (error) {
if (error) {
console.log(error)
}
res.send({
success: true,
message: 'PC saved successfully!',
PC: req.body
})
})
})
})
推荐阅读
- linux - 如何拆分 tar 文件并捕获日志中的 tar 错误?
- docker - 导出的 golang 变量中说明的正在运行的 docker 容器的最新列表
- java - 使用spring框架RestClient调用HTTP请求在使用JRE 10签名的JAR中非常慢
- reactjs - 反应原生双向数据流?
- docker - 从 gitlab 主机连接到 docker 容器的服务端口导致连接被拒绝
- c# - Python 单行和多行注释的正则表达式
- sapui5 - SAPUI5 从 xml 视图调用方法的模型
- colors - 如何从按钮为 datagrid Livecode 中的列着色?
- kubernetes - 关于安装 Calico for Kubernetes 的问题
- javascript - 在不计算包含的 html 标签的情况下查找字符串的长度