- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,当我执行 findByIdAndUpdate 时,它不会按预期执行我的 promise 并进入我的捕获。我使用 res.json(req.user.id)
和 res.json(profileFields)
向 postman 发送了回复。这是我使用时得到的响应个人资料字段
{
"user": "5b3134a0e2543b06d130a5d7",
"handle": "wadeaston1",
"status": "Developer",
"skills": [
"HTML",
" CSS",
" Javascipt"
],
"social": {}
}
我在这里不知所措,因为我的所有字段都按预期将值传递到 user 和 $set 中。我不明白为什么它会成为我的目标
Profile.findByIdAndUpdate(
{ user: req.user.id },
{ $set: profileFields },
{ new: true }
)
.then(profile => res.json(profile))
.catch(err => {
res.json("Timeout");
console.log("HI");
});
这是我的个人资料架构
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
//Create Scheme
const ProfileSchema = new Schema({
user: {
//this will associate user by their ID
type: Schema.Types.ObjectId,
ref: "users"
},
handle: {
type: String,
required: true,
max: 40
},
company: {
type: String
},
website: {
type: String
},
location: {
type: String
},
status: {
type: String,
required: true
},
skills: {
//Array of strings
type: [String],
required: true
},
bio: {
type: String
},
githubusername: {
type: String
},
experience: [
{
title: {
type: String,
required: true
},
company: {
type: String,
required: true
},
location: {
type: String
},
from: {
type: Date,
required: true
},
to: {
type: Date,
required: true
},
current: {
type: Boolean,
default: false
},
description: {
type: String
}
}
],
education: [
{
school: {
type: String,
required: true
},
degree: {
type: String,
required: true
},
fieldofstudy: {
type: String,
required: true
},
from: {
type: Date,
required: true
},
to: {
type: Date,
required: true
},
current: {
type: Boolean,
default: false
},
description: {
type: String
}
}
],
social: {
youtube: {
type: String
},
twitter: {
type: String
},
facebook: {
type: String
},
linkedin: {
type: String
},
instagram: {
type: String
}
},
date: {
type: Date,
default: Date.now
}
});
module.exports = Profile = mongoose.model("profile", ProfileSchema);
最佳答案
findByIdAndUpdate
用于通过其 _id
值查找要更新的文档,但您需要通过其 user
字段查找文档,因此你应该使用findOneAndUpdate
相反:
Profile.findOneAndUpdate(
{ user: req.user.id },
{ $set: profileFields },
{ new: true }
)
.then(...
无需手动将 req.user.id
转换为 ObjectId,因为 Mongoose 会根据您的架构中定义 user
的方式为您执行此操作。
关于javascript - findByIdAndUpdate 不断被捕获在 .catch 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51030772/
findByIdAndUpdate() 成功更新文档,但返回我不明白的错误。 这是架构: const userSchema = mongoose.Schema({ phone: String,
这个问题在这里已经有了答案: Mongoose: findOneAndUpdate doesn't return updated document (16 个答案) 关闭 5 年前。 所以我有这个
似乎无法理解为什么 findByIdAndUpdate 没有更新数据。我将其更改为 findByIdAndRemove 以查看是否可以发现错误,但是当我更改它时,它确实被删除了,因此我无法找到问题所在
这是我的代码: app.post('/api/scheduleObject_Update', function(req, res) { var parameter = req.para
我正在尝试找到一种一致的方法来验证我保存到数组中的一些 id。 架构: var MySchema = new Schema({ name: { type: String }, otherIds
这是我的架构: var postSchema = new mongoose.Schema({ _id: mongoose.Schema.ObjectId, title: String,
我在下面有一个相当直接的方法来更新基于其 ObjectId 的文档。它不会返回错误,但无法对文档进行所需的更新。我认为它失败了,因为根据我的研究,findByIdAndUpdate() 仅采用纯 Ja
我有一个模式并指定登录是唯一的是真的。当我使用 findByIdAndUpdate 并传递查询 $set 来更新用户对象时,它确实不在登录被复制时抛出错误。有谁知道为什么以及如何更新对象并强制执行架构
我正在尝试使用 mongoose 的 findByIdAndUpdate 方法。当我更新具有 _id 字段集的现有文档时,效果很好。但是,插入一个新文档,使用 upsert: true 插入一个没有生
我是 Mongodb 新手,我正在使用 findByIdAndUpdate 它将更新文档但返回旧文档,这是我的功能, exports.createInner = function (req, res,
因此,当我执行 findByIdAndUpdate 时,它不会按预期执行我的 promise 并进入我的捕获。我使用 res.json(req.user.id) 和 res.json(profil
我的想法很简单,就是从req.body中获取更新值,但是它不能正常工作,mongodb中的数据永远不会改变。 已尝试过 {$set: 电子邮件、密码} const id = { _id: re
我通过创建 API 并在 Angular 2 服务中调用它们,根据 Nodejs 中的 id 更新产品。 routes/products.js router.post('/:id', function
我有一个名为 Message 的 Mongoose 模型,当创建该模型时,我想将消息 Id 保存到另一个名为 User 的模型中的数组中。但是,当调用 findByIdAndUpdate 方法时,它不
findByIdAndUpdate 方法应该更新或插入数据库中的对象,但它不执行任何操作。它也不会抛出错误消息或其他内容。 我也尝试过使用原始的 ObjectId 作为 _id 字段,但也不起作用。
所以我只想更新传递的对象的值。 Event.findByIdAndUpdate(req.body.id, { $set: {
开发 Node.js API。我有显示 field 的 mongo 数据。它包含一个地址,例如...... "address": { "street": "123 main s
我试图在用户点击某个按钮时增加赞成票,并将他的 stormpath 用户 ID (req.user.href) 存储在一个数组中。 尝试 1: if (newUpvoters = found_post
我想知道是否有最好的方法来做到这一点: /** * Article Schema */ var PostSchema = new Schema({ title: { typ
我写了一个更新查询,它将通过 _id 找到记录并更新。这段代码完全可以正常工作,直到我在该集合中添加一条新记录。如果添加一条记录并进行查询,它将转到成功部分但返回 null 而不是数据。但是当我重新启
我是一名优秀的程序员,十分优秀!