Giter VIP home page Giter VIP logo

Comments (6)

aalfiann avatar aalfiann commented on July 29, 2024

Please try to remove .exec()

is it works?

from recachegoose.

dnish avatar dnish commented on July 29, 2024

I'll check it. The confusing thing is that some documents are returned correctly, some of them not. user was always defined, but sometimes it was returned as a function (I've logged typeof user).

from recachegoose.

dnish avatar dnish commented on July 29, 2024

Removing exec didn't change anything. I did the following to log the object:

 if(!user.save) {
                        console.log(typeof user);
                        console.log(JSON.stringify(user));
                    }

It returns an object with the user information, but save method isn't defined, so I guess hydration isn't working correctly? But this happens only on some documents.

from recachegoose.

aalfiann avatar aalfiann commented on July 29, 2024

I thought it because

  1. .exec() is for callback use., so you don't need to await;
  2. findOne is for query the document, but save() is for insert new document.

How about this?

 const user = await this.findOne({_id: visitorId}).cache(300,cacheKey);
      if(user) {
       console.log(user)
       }

it just weird, cos you want to search document, but you also want to save the document on the same time also there is no object being save in your sample code.

from recachegoose.

dnish avatar dnish commented on July 29, 2024

Hey,
no, exec() isn't for callback, it returns a promise. The code worked with the old cachegoose before, but we changed because it isn't maintained anymore.

We check with findOne if we already have a user with that specific visitorId. If we have one, we will check when he last visited the page and update his value when the last visit is greater than 30 minutes, that is why we call the save method.

So basically we are doing the following:


 const user = await this.findOne({_id: visitorId}).cache(300,cacheKey).exec(); // Returns a promise
 if(user && user.lastVisit >= CHECK_DATE) {
    user.lastVisit = new Date();
    user.save(); // Here we get the error, that the save method isn't available, which means we don't get a mongoose document returned, instead a raw Javascript object
  }

We are getting the same error also on other Models, the only thing that they have in common is that we use the findOne() method, the normal find() method seems to work fine. Maybe it's related with #2 ?

from recachegoose.

aalfiann avatar aalfiann commented on July 29, 2024

Yes you're right, The cache function not found because the user variable will not return as mongoose Model.

Actualy you don't need a cache when you create or update the document. Why you want to cache if the document will going to change? it won't give you a benefit of cache.

So the solution is try to change your code like this

const updated = await this.findOneAndUpdate({
    _id: visitorId,
    lastVisit: { $gte: CHECK_DATE }
  }, {
    lastVisit: new Date()
  }, { new: true }).catch(err => {
    return console.log(err);
  });

if (updated) {
  console.log(updated);
};

from recachegoose.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.