Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.findOne(condition, selects, { session }) error mongodb4.2 #6

Closed
nxg916 opened this issue Sep 19, 2019 · 2 comments
Closed

Model.findOne(condition, selects, { session }) error mongodb4.2 #6

nxg916 opened this issue Sep 19, 2019 · 2 comments

Comments

@nxg916
Copy link

nxg916 commented Sep 19, 2019

return await Article.findOne({ _id: articleId, book, uuid }).session(session);

"MongoError: Read preference in a transaction must be primary, not: secondary

@dtboy1995 dtboy1995 added bug Something isn't working help wanted Extra attention is needed and removed bug Something isn't working help wanted Extra attention is needed labels Jan 7, 2020
@dtboy1995
Copy link
Member

@naxiaoguang 时间太久了,不好意思,一直没有时间
今天测试了下

环境

  • run-rs 4.2.0
  • Windows10
  • Node 12.14.0

代码

const mongoose = require('mongoose')
const Acid = require('mongoose-acid')
const os = require('os')
const hostname = os.hostname()

mongoose.set('debug', false)
Acid.set('debug', false)

const uri = os.platform() === 'win32' ? `mongodb://${hostname}:27017,${hostname}:27018,${hostname}:27019/test` : 'mongodb://localhost:27017,localhost:27018,localhost:27019/test'
let conn

const Account = mongoose.model('Account', new mongoose.Schema({
    balance: Number
}))
const People = mongoose.model('People', new mongoose.Schema({
    name: String,
    balance: Number
}))

async function test() {
    let conn = await mongoose.connect(uri, {
        replicaSet: 'rs',
        useUnifiedTopology: true,
        useNewUrlParser: true
    })

    // await new Account({ balance: 300 }).save()

    await Acid(async (session) => {
        let account = await Account.findOne({ _id: '5e1438aef13bc455b07bf22d' }).session(session)
        account.balance += 300
        await account.save()
        await new People({ balance: account.balance }).save({ session })
        throw new Error('THROW ERROR')
    })

    await conn.disconnect()
}
test()

并没有发现你出现的问题,你的问题应该和这个库没有关系,应该是你mongodb连接的配置问题?

@dtboy1995
Copy link
Member

image
看下这些副本集连接的配置,确定下您的问题吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants