0

I am using Node.js with mongoose together with Typescript. And based on SO threads like this queries should work with my function as well:

 let conditions = {};

 conditions['belongsToRestaurant'] = dto.restId;
 conditions['belongsToRestaurant']

    await Order.findOne({conditions}).then((doc) => {
                console.log('doc');
                console.log(doc);
            })

The console always logs the proper value for "restId", and always null for "doc".

I have tried every single variation I could think of. I created an object with a constructor, created an object with a set property, tried with classes nothing worked. I tried using mongoose types, such as:

conditions['consumerName'] = new mongoose.Schema.Types.String("asasd");
conditions['belongsToRestaurant'] = mongoose.Types.ObjectId(dto.restId);

Nothing has worked so far. Thx for reading and help in advance!

  • Sidenote: Keep in mind these fields I am referencing are not in array, or in a subdocument or anything of that nature. And work completely fine when I am not using objects to update.
4
  • conditions is already an object and u wrap it in curly braces in findOne which makes it an object inside an object. try removing the curly braces from your findOne function Commented Oct 16, 2018 at 9:31
  • Oh wow, I knew when I woke up this day is gon' be tough. Thanks Asaf Aviv ! It worked! :) How do I mark your comment as a resolution to the problem? Commented Oct 16, 2018 at 9:34
  • lol, glad to help Commented Oct 16, 2018 at 9:35
  • thx, if you can mark your answer as a resolution by any chance go for it. The button does not seem to appear for me for some reason. Commented Oct 16, 2018 at 9:38

1 Answer 1

1

Just a tiny mistake of wrapping the condition object inside an object.

Removing the wrapping curly braces from the findOne function will fix the problem.

const conditions = {};
conditions.belongsToRestaurant = dto.restId;

await Order.findOne(conditions)
  .then((doc) => {
    console.log('doc');
    console.log(doc);
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.