我想將元素推入嵌套元素內的陣列中。
有人問過類似的問題。我嘗試了解決方案,但沒有為我作業。
錯誤資訊是
MongoServerError: Cannot create field 'approved' in
element {transactionList: [ { id: 0, greeterAddress: "0x0", executed: 0, value:
"namaste", approved: [ "0xDcb980b3d6Bd61a86A8d7Cb9bbd937452f7E745D",
"0xDcb980b3d6Bd61a86A8d7Cb9bbd937452f7E745D" ], _id:
ObjectId('62275a5b1722b740ade011ad') } ]}
檔案架構
const transaction = new mongoose.Schema({
id: {type: Number, required:true},
greeterAddress: {type:String, required:true},
executed: {type:Number, required:true},
value: {type:String, required:true},
approved:{type: [String]}
})
const walletInfo = new mongoose.Schema({
walletAddress: {type: String, required: true},
networkName: {type: String, required:true},
minApproval: {type: Number, required:true},
ownerAddress: {type: [String]},
greeter: {type: [String]},
transactionList: {type:[transaction]}
})
執行的代碼給出了錯誤
app.post('/approve_transaction', async(req,res)=>{
walletAddress = req.query.address
id = req.query.id
greeterAddress = req.query.greeterAddress
await walletInfo.updateOne(
{
walletAddress:walletAddress,
'transactionList.id':id,
'transactionList.greeterAddress':greeterAddress
},
{
$push:{
'transactionList.approved':walletAddress
}
})
res.sendFile('success')
})
檔案如下
{
"_id" : ObjectId("62275a0f1722b740ade011a9"),
"walletAddress" : "0xDcb980b3d6Bd61a86A8d7Cb9bbd937452f7E745D",
"networkName" : "4",
"minApproval" : 1,
"ownerAddress" : [
"0x6ED8cB56eF8602e138E7e588EA65A5b33f402087",
"0x6cb691b71CB057e3C92DBB729580e1A7A2868e1c"
],
"greeter" : [],
"transactionList" : [
{
"id" : 0,
"greeterAddress" : "0x0",
"executed" : 0,
"value" : "namaste",
"approved" : [],
"_id" : ObjectId("62275a5b1722b740ade011ad")
}
],
"__v" : 0
}
uj5u.com熱心網友回復:
通過替換解決的 transactionList.approved問題transactionList.$.approved
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/443299.html
標籤:javascript 节点.js mongodb 猫鼬
