我已经这样配置令牌:
jwt.sign(
{
user: pick(user, ['_id', 'username'])
},
secret,
{
expiresIn: '2m'
}
);
Run Code Online (Sandbox Code Playgroud)
但是当我想检查令牌是否已过期时,此代码不起作用
function isAuthenticated() {
const token = localStorage.getItem('token');
const refreshToken = localStorage.getItem('refreshToken');
try {
decode(token);
const { exp } = decode(refreshToken);
if (exp < (new Date().getTime() + 1) / 1000) {
return false;
}
} catch (err) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
问题是这部分:
if (exp < (new Date().getTime() + 1) / 1000) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
新的Date()。getTime()+1)/ 1000 = 1531335468.113
exp = 1531334595
因为我不知道JWT使用哪种时间格式... …
我在 _app.js 组件中使用 getInitialProps,但是当我重新加载页面时,请求不会执行。
例如:
// getData.js
import * as axios from 'axios';
export default async function getData() {
const response = await axios.get('http://someapi.com/');
return response.data;
}
Run Code Online (Sandbox Code Playgroud)
然后我将使用这些数据......
// _app.js
import getData from './getData';
import App, { Container } from "next/app";
class MyApp extends App {
static async getInitialProps() {
const response = await getData();
if (response) {
return { response };
}
return {};
}
render() {
const { Component, response } = this.props;
<Container>
<Component {...this.props} data={response} /> …Run Code Online (Sandbox Code Playgroud) 我正在使用 MongoDB Atlas 来托管数据库,并使用这个无服务器函数查询数据:
import { NextApiRequest, NextApiResponse } from "next";
// models
import { Section } from "../../../models/Section";
// db connection
import { useDatabase } from "../../../middleware/useDatabase";
async function find(req: NextApiRequest, res: NextApiResponse) {
const { content } = req.query;
const response = await Section.find({ contentType: content });
res.send(response);
}
export default useDatabase(find);
Run Code Online (Sandbox Code Playgroud)
中间件useDatabase配置如下:
import * as mongoose from "mongoose";
import { NextApiRequest, NextApiResponse } from "next";
export const useDatabase = handler => async (req: NextApiRequest, res: …Run Code Online (Sandbox Code Playgroud) 如何使用最新版本的 TypeORM 在 MySQL 中进行全文查询?
我想搜索所有具有确定单词或短语的人。例如,在表“人”中有:
@Column("varchar")
name: string;
@Column("varchar")
lastname: string;
@Column("text")
personalDescription: string;
Run Code Online (Sandbox Code Playgroud)
因此,如果我输入“Andrés”、“Niko”、“我是开发人员”或其他任何内容,我想通过 MySQL 的“全文”功能在用户的姓名、姓氏和个人描述中找到谁拥有该信息。
谢谢。
我想将 MySQL 与 Django 集成,MySQL 在 Docker 上运行,我将这样的配置与 Django 连接到 db docker:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'restaurant',
'HOST': 'db',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'root',
'default-character-set': 'utf8',
'OPTIONS': {
'init_command': 'SET default_storage_engine=INNODB',
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当 Django 尝试连接到 mysql 数据库时,它会抛出以下错误:
我尝试使用 pip 安装 mysqlclient,但出现此错误:
这些是docker-compose.dev.yml和Dockerfile配置。
如果有人需要完整的代码,您可以在这里找到它,并使用docker-compose -f docker-compose.dev.yml up --build.
谢谢 :)。