javascript – 更新使用Mongoose模型检索并作为承诺处理的文档时出现问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 更新使用Mongoose模型检索并作为承诺处理的文档时出现问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5262字,纯文字阅读大概需要8分钟。
内容图文
跳转到更新#2以获得更详细的信息
我对从通过Mongoose模型查询容器检索到的文档进行简单更新时遇到问题.查找查询确实有两个群体,但除此之外,我不确定问题是什么
我遇到的问题是,当我检索文档,更新属性,然后尝试通过Mongooses Doc.save()方法将更新保存到文档时,似乎没有任何事情发生.什么是奇怪的,是save()甚至没有触发传递给它的回调..(或者如果我作为Promise处理它,则触发then()或catch())
型号:资产
module.exports = Mongoose => {
const Schema = Mongoose.Schema
const assetSchema = new Schema({
attributes: [{
_field: {
type: Schema.Types.ObjectId,
ref: 'Field',
required: true
},
value: {
type: Schema.Types.Mixed,
required: true
}
}],
_partition: {
type: Schema.Types.ObjectId,
ref: 'Partition'
}
})
return Mongoose.model( 'Asset', assetSchema )
}
而且仅仅是为了一些细节,下面是来自相同查找的具有相同两个群体的示例结果
[
{
"_id" : ObjectId("56b626dc4040b5383696d16f"),
"_partition" : {
_fields: [Object],
name: 'Server stuff',
_id: ObjectId("56ae4707f3d4645b2e5b0537")
},
"attributes" : [
{
_field: {
_id: ObjectId("56ae4703f3d4645b2e5b0534"),
name: 'Hostname'
},
value: 'server-04.foobar.ad',
_id: ObjectId("56b626dc4040b5383696d172")
}
]
}
]
文档查询
下面是我通过Foo.find方法检索一些文件的代码示例(有效),并更新第一个属性(我的工作)的值,但是当我尝试a.save()时,doc …没什么发生:
Asset
.find( { _id: '56b6568cb5195902381f6c65' } )
.populate( { path: 'attributes._field' } )
.populate( { path: '_partition' } )
.then( docs => {
if( ! docs )
throw new Error(`No docs were found`)
console.log('# Docs Found:', docs.length) // 1
console.log('# Instance?:', docs[0] instanceof Asset) // true
console.log('# Docs:', assets) // Shows single document inside array
let a = docs[0]
a.attributes[0].value = 'blah'
// This is where the problem is, nothing below here is displayed in the console
a.save(function (err) {
if (err)
throw new Error('Failed to update:' + err)
console.log('Updated!')
})
} )
.catch( err => console.error( 'ERROR:',err ) )
.finally( () => Mongoose.connection.close() )
在控制台中,一切都按预期显示,直到a.save()..既不是错误也不是更新!被展示.
它绝对是我正在与之交互的Mongoose文档(Foo的一个实例显示为true),所以我不确定为什么save()没有做任何事情.
我试图将a.save()作为Promise来处理,而不是将回调交给它,再一次,没有任何事情发生,也没有执行then或catch.
这真让我抓狂!!我确定我忽略了一些愚蠢的东西,但我似乎无法找到它.任何帮助都会被激活
附:我没有包含分区或字段模型/模式,因为我高度怀疑它们是否相关…但如果有人这么认为,请告诉我
P.S.S.只是一个FYI,MongoDB用户确实具有写访问权限
更新#1
根据@JohnnyHK的建议,我尝试执行Doc.markModified():
Asset
.find( { _id: '56b6568cb5195902381f6c65' } )
.populate( { path: 'attributes._field' } )
.populate( { path: '_partition' } )
.then( docs => {
if( ! docs )
throw new Error(`No docs were found`)
console.log('# Docs Found:', docs.length) // 1
console.log('# Instance?:', docs[0] instanceof Asset) // true
console.log('# Docs:', assets) // Shows single document inside array
let a = docs[0]
a.attributes[0].value = 'blah'
a.markModified('attributes')
// This is where the problem is, nothing below here is displayed in the console
a.save(function (err) {
if (err)
throw new Error('Failed to update:' + err)
console.log('Updated!')
})
} )
.catch( err => console.error( 'ERROR:',err ) )
.finally( () => Mongoose.connection.close() )
没有更改…控制台中没有显示a.save()中的任何内容,并且文档未更新
更新#2
经过一些修修补补……似乎与它有关…
这很成功:
// AS A CALLBACK
Asset.find( { _id: '56b6568cb5195902381f6c65' } )
.populate( { path: 'attributes._field' } )
.populate( { path: '_partition' } )
.exec(function (err, doc) {
if (err) throw new Error(err)
doc[0].attributes[0].value = 'FOO'
doc[0].save(function (err) {
if (err) throw new Error(err)
console.log('Updated to:',doc[0])
Mongoose.connection.close()
})
})
这是不成功的:
// AS A PROMISE
import Promise from 'bluebird'
Mongoose.Promise = Promise
// ..
Asset.find( { _id: '56b6568cb5195902381f6c65' } )
.populate( { path: 'attributes._field' } )
.populate( { path: '_partition' } )
.then( doc => {
doc[0].attributes[0].value = 'FOO'
doc[0].save(function (err) {
if (err) throw new Error(err)
console.log('Updated to:',doc[0])
})
})
.catch( err => {
throw new Error(err)
})
.finally( () => Mongoose.connection.close() )
我能想到的唯一区别是承诺,而不是回调
什么超级奇怪…… .then()中的一些代码执行..只是不是save()
更新#3
我创建了一个github issue,并根据我的上次更新..
我找到了this issue,他的“解决方案”是将整个主要版本从4.X降级到3.X ……
我目前的版本是^ 4.3.7,我试图将其更改为3.8.35,降级顺利然后脚本本身就抛出了一堆错误……老实说,id真的没有使用这么老的版本反正.
解决方法:
我使用您提供的架构复制了您遇到的问题并找到了解决方案.以下内容适用于我并更新了.value属性.尝试在返回doc [0] .save()之前返回它,如this article中所述
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
// .....
Asset.find( { _id: '56b6568cb5195902381f6c65' } )
.populate( { path: 'attributes._field' } )
.populate( { path: '_partition' } )
.exec() // Queries aren't full-fledged promises!
.then( doc => {
console.log('Before:',doc[0]);
doc[0].attributes[0].value = 'FOO'
// Return a promise!
return doc[0].save(function(err){
console.log('Updated to:', doc[0]);
});
})
.catch( err => {
throw new Error(err)
})
.finally( () => Mongoose.connection.close() )
我也使用.exec()因为根据文档queries aren’t full-fledged promises(以防万一).由于.save()返回一个promise,你应该返回它,以便promises按顺序解析;在执行之前等到先前的承诺完成.发布代码here
内容总结
以上是互联网集市为您收集整理的javascript – 更新使用Mongoose模型检索并作为承诺处理的文档时出现问题全部内容,希望文章能够帮你解决javascript – 更新使用Mongoose模型检索并作为承诺处理的文档时出现问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。