检查数组中的任何项是否等于此值

时间:2016-12-14 21:47:56

标签: javascript arrays angular rxjs

此功能将用户记录为已阅读该页面,并在用户点击下一页时触发。

首先订阅数据库并检索用户已读过的所有页面的列表。

我的目标是仅记录用户未读取的页面,例如不在数据库中。

如果从数据库返回的列表包含与刚刚读取的页面相同的页码,则此函数不会将页面记录为正在读取。

然而,这仅适用于第一次点击,然后如果我再次返回同一页面并单击下一步,它会将页码记录到数据库中...

有关如何修复此错误的任何建议,需要另外关注此代码我知道它的简单事项。干杯!

/********************** APP DEPENDENCES AND CONFIGURES ************************/
// Include required modules
var compression = require('compression');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var MYSQL_db = require('./models/database/MYSQL');
var helper = require('./models/utils/functions');
var util = require('util');
require('./models/utils/extend_prototype');

// Include global configure file
var GLOBAL_VAR = require('./config/global');

// Include environmental specific configure file
switch (process.env.NODE_ENV) {
    case 'development':
        var ENV_VAR = require('./config/dev');
        app.locals.url_prefix = ENV_VAR.URL_PREFIX_PATH;
        break;
    case 'production':
        var ENV_VAR = require('./config/production');
        app.locals.url_prefix = ENV_VAR.URL_PREFIX_PATH;
        break;
    default:
        console.error("Unrecognized NODE_ENV: " + process.env.NODE_ENV);
        process.exit(1);
}

// Configure express static files and template language to use
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.use(express.static('public'));

// Configure the middlewares
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Set global app variables
var LAST_RECEIVED_POST_ID = 0;

// Database connection
var mysql = new MYSQL_db({
    host: ENV_VAR.MYSQL.host,
    user: ENV_VAR.MYSQL.user,
    password: ENV_VAR.MYSQL.password,
    database: ENV_VAR.MYSQL.database
});

// 'Can't set headers after they are sent', fix
app.use(function(req, res, next) {
    var _send = res.send;
    var sent = false;
    res.send = function(data) {
        if (sent) return;
        _send.bind(res)(data);
        sent = true;
    };
    next();
});

/********************** APP DEFINED MIDDLEWARES *******************************/

// -- Index Middleware --
// 1. Get Feature post
// 2. Get other posts by date
// 3. Update LAST_RECEIVED_POST_ID
// 4. Request blog admin profile
// 5. Render the index page
app.get(ENV_VAR.URL_PREFIX_PATH + '/', function(req, res) {
    mysql.select_post('featured', function(err, featured_post) {
        if (err) throw err;
        mysql.select_post({
            status: 'published',
            limit: 10
        }, function(err, posts_res) {
            if (err) throw err;
            var posts = [];
            if (posts_res.length > 0 || featured_post.length > 0) {
                posts = helper.prepare_index_post_data(posts_res, featured_post[0]);
                LAST_RECEIVED_POST_ID = posts[posts.length - 1].post_ID;
            }
            mysql.select_author({
                role: 'admin',
                email: GLOBAL_VAR.ADMIN
            }, function(err, author_res) {
                if (err) throw err;
                res.render('index', {
                    _POST_LIST: posts,
                    _ADMIN_AVATAR: author_res[0].author_avatar
                });
            });
        });
    });
});


/*********************  HANDLE COMMON HTTP ERRORS *****************************/
app.get('/404', function(req, res, next) {
    next();
});

app.get('/403', function(req, res, next) {
    var err = new Error('You have no permission to enter that area.');
    err.status = 403;
    next(err);
});

app.get('/500', function(req, res, next) {
    next(new Error('keyboard cat!'));
});

app.use(function(req, res, next) {
    res.status(404);
    res.render('errors/404');
});

/***********************  START THE APP  **************************************/
app.listen(GLOBAL_VAR.PORT, function() {
    console.log('The Wall personal blog by George G. Gkasdrogkas.');
    console.log('Listening on port ' + GLOBAL_VAR.PORT + '!');
});

}

logAsWatched() {
let uid = this.windowRef.nativeWindow.firebase.auth().currentUser.uid;
this.projectId = this.uidpage.project.id;
this.subscription = this.af.database.list(`pagelogs/${uid}/${this.projectId}`).subscribe(
  data => {
    console.dir(data);
    if (data.length >= 1) {
      let pagelogArray = [];
      for (let i = 0; i < data.length; ++i) {
        let pageNumber = data[i];
        pagelogArray.push(pageNumber);
      }
      console.log('pagelogArray', pagelogArray);
      console.log('page order', this.pageOrder);

      let newArray = [];

      for (let i = 0; i < pagelogArray.length; ++i) {
        newArray.push(pagelogArray[i].pNumber);
      }
      console.log(`newArray : ${newArray}`);
      if (!newArray.indexOf(this.pageOrder)) {
        console.log('page has allready been logged');
      } else {
        console.log('logging this page as watched');
        this.setLogAsWatchedData();
      }
    } else {
      console.log('No page logs have been added');
      this.setLogAsWatchedData();
    }
  }, (error) => { console.log(`page log error: ${error}`); }
);

1 个答案:

答案 0 :(得分:1)

我认为你可以研究一下如何使用Filter [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter]来输出以read而不是read方式记录的页面。