简体   繁体   English

[StitchServiceError: Error: ObjectId in must be a single string of 12 bytes or a string of 24 hex characters]

[英][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.

相关问题 传入的参数必须是12字节的单个String或24个十六进制字符的字符串,Mongoose ObjectId err - Argument passed in must be a single String of 12 bytes or a string of 24 hex characters, Mongoose ObjectId err Mongoose 错误:传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串 - Mongoose Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters Monogo DB 更新错误:传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串 - Monogo DB Update Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters Express 500错误:传入的参数必须是12个字节的单个字符串或24个十六进制字符的字符串 - Express 500 Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters 错误:传入的参数必须是12字节的单个字符串或24个十六进制字符的字符串,它在mongodb和节点中 - Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters, its in mongodb and node 错误:传入的参数必须是 12 字节的单个字符串或 ID 错误的 24 个十六进制字符的字符串 - Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters with wrong ID 错误删除。 MongooDB + Express。 传入的参数必须是12个字节的单个字符串或24个十六进制字符的字符串 - Deleting with error. MongooDB + Express. Argument passed in must be a single String of 12 bytes or a string of 24 hex characters typescript 传入的参数必须是单个 12 字节的字符串或 24 个十六进制字符的字符串 - typescript Argument passed in must be a single String of 12 bytes or a string of 24 hex characters mongdb和尚-传入的参数必须是12个字节的单个字符串或24个十六进制字符的字符串 - mongdb monk -Argument passed in must be a single String of 12 bytes or a string of 24 hex characters TypeOrm findOne 抛出“传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串” - TypeOrm findOne throws 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM