最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在express js中有特殊条件后有执行函数的方法

网站源码admin14浏览0评论

在express js中有特殊条件后有执行函数的方法

在express js中有特殊条件后有执行函数的方法

例如,我有一个这样的用户模型

const mongoose = require("mongoose");
     const Schema = mongoose.Schema;
     username: {
          type: String,
          required: true,
          },
      Points: {
          type: Number,
          default: 0,
          },
module.exports = User = mongoose.model("users", UserSchema);

然后,我想在express.js的user.points等于10时自动执行一个函数,有什么解决方案吗?

回答如下:

@ Yessine,请您尝试这样的事情,

const { Users } = require('/schema.js');

const checkForPoints = async (username) => {
    await Users.findOne({ username }, function (err, data) {

        if (err) {
            console.log("enter error ------", err)
        }

        if (data && data.Points === 10) {
            // Execute your code
        }
    });
};

// Users schema(schema.js)
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('your db', { useNewUrlParser: true });
const requestSchema = mongoose.Schema({
    _id: mongoose.Types.ObjectId,
    username: String,
    Points: Number
});

module.exports = mongoose.model('users', requestSchema);

轮询是一种通过定期向服务器发出API请求来检查给定时间间隔内新数据的技术。使您能够定期检查数据的最新值,并在数据进入所需状态后进行进一步的请求。

发布评论

评论列表(0)

  1. 暂无评论