[英][StitchServiceError: Error: ObjectId in must be a single string of 12 bytes or a string of 24 hex characters]
I am writing Mongodb Stitch, functions.我正在编写 Mongodb Stitch,函数。
The weird part is, writing it within Mongodb Stitch function client, it has to do BSON.ObjectId奇怪的部分是,在 Mongodb Stitch 函数客户端中编写它,它必须执行 BSON.ObjectId
const games = mongodb.collection("games");
const gameId = "5cb9404ffc6da85909eb561c";
const objectId = BSON.ObjectId(gameId);
const query = { "_id" : objectId };
return games.findOne(query);
But, when I use my app to pass the arg
to the function, it will complain [StitchServiceError: Error: ObjectId in must be a single string of 12 bytes or a string of 24 hex characters]
但是,当我使用我的应用程序将
arg
传递给函数时,它会抱怨[StitchServiceError: Error: ObjectId in must be a single string of 12 bytes or a string of 24 hex characters]
Which I have to revert it back to我必须将其恢复为
const gameId = "5cb9404ffc6da85909eb561c";
const query = { "_id" : gameId };
return games.findOne(query);
Which this will return null
if I test it within MongoDB Stitch function client.如果我在 MongoDB Stitch 函数客户端中测试它,这将返回
null
。
Why MongoDB designs in this way?为什么MongoDB这样设计?
For app to work, you only need to pass the id string. 要使应用程序正常工作,您只需传递id字符串即可。
const gameId = "5cb9404ffc6da85909eb561c";
const query = { "_id" : gameId };
return games.findOne(query);
This worked for me:这对我有用:
exports = function(payload, response) {
const mongodb = context.services.get("mongodb-atlas");
const collection = mongodb.db("mydatabase").collection("mycollection");
// const id = "5f2b7706fae92314e01a2c3c";
// let cursor = collection.findOne({_id: BSON.ObjectId(id)});
let cursor = collection.findOne({_id: BSON.ObjectId(payload.query.id)});
cursor.then(function(result){
response.setStatusCode(200);
response.setHeader(
"Content-Type",
"application/json"
);
response.setBody(JSON.stringify(result));
});
};
Then to test:然后进行测试:
https://yourwebhookurl?id=5f2b7706fae92314e01a2c3c
Also see: https://intercom.help/mongodb-atlas/en/articles/1566512-how-do-i-work-with-objectid-in-stitch另见: https : //intercom.help/mongodb-atlas/en/articles/1566512-how-do-i-work-with-objectid-in-stitch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.