How to use the mongodb.ObjectID.isValid function in mongodb

To help you get started, we’ve selected a few mongodb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ashokdey / LearningNode / mongo-rest-api / app / server.js View on Github external
app.get('/todos/:id', authenticate,  (req, res) => {
    //get the todo id in todoID
    let todoID = req.params.id;

    // if the ID is valid not valid, send a message
    if (!ObjectID.isValid(todoID)) {
        //console.log('Invalid todo ID');
        return res.status(400).send({
            message : 'InvalidID',
            status : 400
        });
    }
    // search for todo with the todo id and the creator
    // do not displa todo if the creator id is different
    Todo.findOne({
        _id : todoID,
        _creator : req.user._id
    }).then((data) => {
        if (!data) {
            return res.status(404).send({
                message : 'Todo not found',
                status : 404
github open-rpa / openflow / OpenFlow / src / DatabaseConnection.ts View on Github external
const safeObjectID = (s: string | number | ObjectID) => ObjectID.isValid(s) ? new ObjectID(s) : null;
export declare function emit(k, v);
github pbatey / express-mongo-rest / index.js View on Github external
function normalizeId(id) {
    if (ObjectID.isValid(id)) return new ObjectID(id)
    return id;
}
github Innovic-io / angular-nestjs-rendering / src / server / modules / pets / pets.resolvers.ts View on Github external
async updatePetsOwner(obj, { petID, owner }, context?, info?) {

    if (!ObjectID.isValid(owner) || !ObjectID.isValid(petID)) {

      throw new ApolloError(WRONG_ID_ERROR);
    }

    const newOwner = await this.ownerService.changeOwner(petID, owner);

    return newOwner.value;
  }
github webdevstar / React-Ecommerce / src / api / server / services / products / products.js View on Github external
setAvailableSlug(product, productId) {
    if(product.slug && product.slug.length > 0) {
      let newSlug = utils.cleanSlug(product.slug);
      let filter = {};
      if(productId && ObjectID.isValid(productId)) {
        filter._id = { $ne: new ObjectID(productId) }
      }

      return mongo.db.collection('products').find(filter).project({slug: 1}).toArray()
        .then(products => {
          while(products.find(p => p.slug === newSlug)) {
            newSlug += '-2';
          }
          product.slug = newSlug;
          return product;
        })
    } else {
      return Promise.resolve(product)
    }
  }
github gadicc / gongo / gongo-server / src / server.js View on Github external
  deconstruct: objId => ObjectID.isValid(objId) && [objId.toJSON()],
  reconstruct: args => args && new ObjectID(args[0])
github webdevstar / React-Ecommerce / src / api / server / services / customers / customers.js View on Github external
deleteAddress(customer_id, address_id) {
    if (!ObjectID.isValid(customer_id) || !ObjectID.isValid(address_id)) {
      return Promise.reject('Invalid identifier');
    }
    let customerObjectID = new ObjectID(customer_id);
    let addressObjectID = new ObjectID(address_id);

    return mongo.db.collection('customers').updateOne({
      _id: customerObjectID
    }, {
      $pull: {
        addresses: {
          id: addressObjectID
        }
      }
    });
  }