我正在為一個簡單的 CRUD 應用程序創建 MongoDB Stitch http 服務(使用 3rd Party Services > Incoming Webhooks > Function Editor),我正在嘗試 insertOne() 文檔,然后將文檔 ID 作為字符串返回。插入按預期工作,我等待結果并將其存儲在變量中。const result = await collection.insertOne(payload.query)const id = result.insertedId問題是試圖將其轉換id為字符串。我猜這是因為我對 MongoDB 擴展 JSON 和 BSON 類型等缺乏了解。我已經閱讀了我認為可以在這里提供解決方案的所有文檔部分。我嘗試了以下方法將返回值轉換為字符串:id.toString() // logs as a string, but is an object { $oid: '507c7f79bcf86cd7994f6c0e' }id.valueOf() // logs as a string, but is an object { $oid: '507c7f79bcf86cd7994f6c0e' }id['$oid'] // undefinedObject.values(id.insertedId) // throws error: {"message":"'values' is not a function"}id.str // undefinedresult.str // undefined不知道還有什么可以嘗試的,如果有人能在這里指出我正確的方向以提供解決方案,我將不勝感激。先感謝您。
使用 MongoDB Stitch webhook 服務函數,如何將返回的“insertedId”
揚帆大魚
2022-05-22 13:56:08