【发布时间】:2020-09-01 12:40:18
【问题描述】:
所以我正在尝试与猫鼬建立一些关系。但是当谈到将一个模式保存到另一个模式时,我得到了这个错误:
Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
这里有一些类似的模式:
mongoose = require('mongoose')
const Schema = mongoose.Schema
const userSchema = new Schema({
_id: String,
role: {
type: String,
default: 'Player'
},
tags: {
type: [Schema.Types.ObjectId],
ref: 'Tag'
}
})
module.exports = mongoose.model('User', userSchema)
mongoose = require('mongoose')
const Schema = mongoose.Schema
const tagSchema = new Schema({
_id: {
type: String,
require: true
},
description: {
type: String,
require: true
},
color: {
type: String,
default: '#2476d1'
}
})
module.exports = mongoose.model('Tag', tagSchema)
以及我如何尝试链接
try {
const tag = await Tag.findOne({_id: 'Noob'})
user.tags.push(tag.id)
await user.save()
} catch (err) {
console.error(`ERROR: ${user._id} at Noob check`)
console.error(err)
}
根据其他帖子,我也尝试过像这样推送_id:
user.tags.push(mongoose.ObjectID(tag._id))
// and
user.tags.push(mongoose.ObjectID.createFromHexString('4e6f6f62')) // just 'Noob' in hex
我仍然得到那个错误。如何使用自定义 _id 创建引用?
【问题讨论】:
标签: node.js mongodb mongoose schema