由于我想使用 React Context 设置 Axios 拦截器,因此似乎可行的唯一解决方案是创建一个拦截器组件,以便使用 useContext 挂钩来访问 Context 状态和分派。
问题是,这会创建一个闭包,并在调用拦截器时将旧数据返回给拦截器。
我正在使用 React/Node 使用 JWT 身份验证,并且我正在使用 Context API 存储访问令牌。
这就是我的拦截器组件现在的样子:
import React, { useEffect, useContext } from 'react';
import { Context } from '../../components/Store/Store';
import { useHistory } from 'react-router-dom';
import axios from 'axios';
const ax = axios.create();
const Interceptor = ({ children }) => {
const [store, dispatch] = useContext(Context);
const history = useHistory();
const getRefreshToken = async () => {
try {
if (!store.user.token) {
dispatch({
type: 'setMain', …Run Code Online (Sandbox Code Playgroud) 随着 Webpacker 加入 Ruby On Rails,我找不到使用 JavaScript 函数的方法。
我有一个app-globals.js用函数调用的文件来测试:
function alerts() {
alert("TEST")
}
Run Code Online (Sandbox Code Playgroud)
然后我想在我的一个观点中使用它:
<% = button_tag 'Button', type: 'button', onClick: 'alerts ()'%>
Run Code Online (Sandbox Code Playgroud)
但是当我按下按钮时,这个错误显示在浏览器控制台中:
ReferenceError: alerts is not defined
我将app-globals.js文件放入"app/javascript"并"app/ javascript/packs/application.js"放入 require ("app-globals")。
我将 app-globals.js 移至 .js"app/javascript/packs"并从application.js.
无论哪种情况,仍然会出现错误。
我正在尝试将 EditorJs 输出转换为 Html。EditorJs 输出“干净”的数据,如下所示:
{
"time": 1589987527499,
"blocks": [
{
"type": "embded",
"data": {
"service": "youtube",
"source": "https://www.youtube.com/watch?v=JbqGYgI3XY0",
"embed": "https://www.youtube.com/embed/JbqGYgI3XY0",
"width": 580,
"height": 320,
"caption": ""
}
},
{
"type": "image",
"data": {
"file": {
"url": "http://localhost/uploads/images/1.jpg"
},
"caption": "",
"withBorder": false,
"stretched": false,
"withBackground": false
}
},
{
"type": "header",
"data": {
"text": "test",
"level": 2
}
}
],
"version": "2.17.0"
}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为原始 HTML?我必须手动转换吗?
我将editorjs配置到我的应用程序中。我在上面安装了图像插件,以便我可以将图像上传到服务器。现在我的服务器上传图像并返回指定格式的 JSON,即
\n\n{\n "success": 1,\n "file": {\n "url": "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg",\n // ... and any additional fields you want to store, such as width, height, color, extension, etc\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n响应有 file->url 但上传消息仍然出错
\n\n
这是我的配置代码:
\n\nconst editor = new EditorJS({\n /**\n * Id of Element that should contain Editor instance\n */\n holder: \'heditor\',\n\n /** \n * Available Tools list. \n * Pass Tool\'s class or Settings object for each Tool you want to use \n …Run Code Online (Sandbox Code Playgroud) 所以我想看看这个新的富文本编辑器编辑器,https://editorjs.io/
我安装了非官方的 reactJS 版本,但我不太确定我在这里做错了什么...... https://www.npmjs.com/package/react-editor-js
有没有人用过这个?可以用钩子完成吗?我的想法是我需要定义这个编辑器的一个实例,以便我可以保存数据。因为当前onChange 编辑器没有向数据对象或输入的数据添加任何新块。
此外,如果我在控制台中将数据对象作为空对象传递,则不会显示初始 EditorJs 块。
任何帮助,将不胜感激。
function App() {
let data = { '1': 'test' }
return (
<div className="App">
<EditorJs
data={data}
onChange={(e) => console.log(data)}
tools={{
code: Code,
header: Header,
paragraph: Paragraph
}}
/>
</div>
);
}
Run Code Online (Sandbox Code Playgroud) 据我了解,回调函数是异步的,不能像常规函数那样返回值。在阅读了 Promise 后,我认为我对它们有了很好的理解,它们基本上是回调的增强版本,允许像异步函数一样返回值。在我的getConnections方法中,我尝试find()通过猫鼬调用数据库上的函数,并且尝试获取该对象数组并将其发送到视图。
var test = new Promise((resolve, reject) => {
Database.find().then(list => {
resolve(list);
}).catch(err=> {
return reject(err);
})
})
console.log(test)
Run Code Online (Sandbox Code Playgroud)
当我尝试在承诺功能之外登录到控制台时,我得到Promise { _U: 0, _V: 0, _W: null, _X: null }
我认为这功能不正确,并且我认为我正确地使用了承诺。有人能指出我如何在回调函数之外返回这个对象数组的正确方向吗?
我想做一些反应角色。但为此,我必须缓存机器人启动之前发送的消息。我尝试过channel.messages.fetch,但到目前为止还没有奏效。
我当前的代码:
client.on('messageReactionAdd', async(reaction, user) => {
client.channels.cache.get("689034237672030230");
channel.messages.fetch('708428887612194979');
// When we receive a reaction we check if the reaction is partial or not
if (reaction.partial) {
// If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
try {
await message.reaction.fetch();
} catch (error) {
console.log('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
// …Run Code Online (Sandbox Code Playgroud) 我正在制作一个 Discord 机器人,有一次我必须用 PCM 格式保存音频
voiceChannel.join().then(connection =>
{
const fs = require('fs');
// Create a ReadableStream of s16le PCM audio
var usee = message.member;
const audio = connection.receiver.createStream(usee, { mode: 'pcm' });
var idAudio = makeid(10);
audio.pipe(fs.createWriteStream(idAudio + '.pcm'));
Run Code Online (Sandbox Code Playgroud)
(idAudio 是随机生成的字符串)。问题是我需要将 PCM 文件转换为 MP3。我已经尝试使用 node-lame 但没有用。我确定我错过了一些东西。PCm 是有符号的 16 位小端 (s16le) PCM
const Lame = require("node-lame").Lame;
const encoder = new Lame({
output: "./audio-files/demox122.mp3",
bitrate: 192
}).setFile("./user_audio.pcm");
encoder
.encode()
.then(() => {
// Encoding finished
console.error("all good");
})
.catch(error => {
// …Run Code Online (Sandbox Code Playgroud) 我是新来的。针对类似问题发布了几个解决方案,但没有一个有帮助。我在下面发布了一个非常简单的玩具示例,可以帮助您进行调试。
索引.js
const express = require('express')
port=3001
var controller = require('./controller');
app.use('/api', controller);
Run Code Online (Sandbox Code Playgroud)
控制器.js
var model = require('./model')
var router = express.Router();
router.get('/bl', function(req, res) {
model.getData( function (err, objects) {
if(err) return res.send(err);
return res.status(200).json(objects);
});
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
模型.js
const bl = {"hello":"world"}
const getData= (request, response) => {
return(bl);
}
module.exports = {
getData
}
Run Code Online (Sandbox Code Playgroud)
问题:调用:http://localhost:3001/api/bl => 无响应,控制台:无错误
笔记:
在我的model.js 中,我在Postgres 数据库中查询,我可以在console.log 中看到结果。
但是当我尝试在 controller.js 中使用 console.log 查看数据时,我没有看到任何这样的结果。我在上面的玩具示例中观察到的类似行为
我收到错误:TypeError: sequelize.transaction is not a function.使用 4.x
const sequelize = require('sequelize');
await sequelize.transaction(async (transaction) => {
await ProjectFile.destroy({
where: projectId,
}
}, { transaction });
});
Run Code Online (Sandbox Code Playgroud)