I have a Vue app running on a VPS Debian server with nginx.
I use the Cache API for caching data and loading it into the app.
When the app was uploaded on my ftp it worked fine, after I deployed it on the server, however,
I receive an error :
Uncaught (in promise) ReferenceError: caches is not defined
The code that runs the cache api is:
const getCachedResource = async (type) => {
const url = `${DATA_URL}${type}.json`
const cache = await caches.open(cacheName);
if (cache) {
const response = await cache.match(url)
if (!response) {
return undefined
}
const jsonResponse = await response.json()
return jsonResponse
}
}
Is there anything I need to enable/fix on the VPS to get it going?
The issue was that the Caches API requires HTTPS. When I added the certificate the issue was fixed.
Related
All apis are working just fine when serving locally but when i try to deploy the app in vercel/ heroku, the apis give me internal error code 500.
So i tried to make a "test api " called hello, it worked just fine until i tried to open connection with db ( query was working locally but in production it gave me internal error)
Here is my code
const mysql= require("mysql2/promise")
export default async function hello(req, res) {
const db= await mysql.createConnection({
host:"eu-cdbr-west-02.cleardb.net",
user:"########",
password:"####",
database:'####',
port:'3306',
})
const [results] = await db.execute("select * from ##",[])
db.end()
res.json(results)
}
Update: On heroku, the api is taking 10 seconds to respond back
I have used create-react-app to build a web app and put the api keys in the .env file in the root folder of my project.
as shown below...
REACT_APP_EDAMAM_API_KEY=***********
REACT_APP_EDAMAM_APP_ID=*********```
however I'm receiving this error when the site is deployed to netlify...
Type Error: failed to fetch
and in the response header it says both the api_key and the app_id are undefined?
here is the api call
const fetchMyApi = async (searchQuery) => {
const API_KEY = process.env.REACT_APP_EDAMAM_API_KEY;
const APP_ID = process.env.REACT_APP_EDAMAM_APP_ID;
const url = `https://api.edamam.com/search?app_id=${APP_ID}&app_key=${API_KEY}&q=`;
const response = await fetch(url + searchQuery);
const data = await response.json();
return data.hits;
};
export default fetchMyApi;
any help would be much appreciated!
Second time posting on StackOverflow so I apologize for any mistakes.
Please bear with me.
Same with the title; How do you read contents of a discord attachment let's say a .txt file and print the contents?
I have tried with fs but unfortunately failed and I have also searched the documentation but failed also.
Ideas?
You can't use the fs module for this as it only deals with local files. When you upload a file to the Discord server, it gets uploaded to a CDN and all you can do is grab the URL of this file from the MessageAttachment using the url property.
If you need to get a file from the web, you can fetch it from a URL using the built-in https module, or you can install one from npm, like the one I used below, node-fetch.
To install node-fetch, run npm i node-fetch in your root folder.
Check out the working code below, it works fine with text files:
const { Client } = require('discord.js');
const fetch = require('node-fetch');
const client = new Client();
client.on('message', async (message) => {
if (message.author.bot) return;
// get the file's URL
const file = message.attachments.first()?.url;
if (!file) return console.log('No attached file found');
try {
message.channel.send('Reading the file! Fetching data...');
// fetch the file from the external URL
const response = await fetch(file);
// if there was an error send a message with the status
if (!response.ok)
return message.channel.send(
'There was an error with fetching the file:',
response.statusText,
);
// take the response stream and read it to completion
const text = await response.text();
if (text) {
message.channel.send(`\`\`\`${text}\`\`\``);
}
} catch (error) {
console.log(error);
}
});
reply to #Andryxa, maybe you can use this with external APIs like a transcription service in case of audio files or to send requests to already created bots from services like dialogflow to replies to the messages
When run bellow code it's give error, Reading file from directory working perfect but when pass url it's give file not found error. I've check fs.statSync accept url.
const stat = fs.statSync('http://techslides.com/demos/sample-videos/small.mp4');
Error: ENOENT: no such file or directory, stat 'http://techslides.com/demos/sample-videos/small.mp4'
fs.statSync() can take a URL, but ONLY if that URL is a file:// URL.
It is not clear what you would want to do if the argument was actually an http:// URL. You could check to see if it was not a file URL and then attempt to fetch the contents of the URL to see if it exists using a library such as got().
But, fetching data from another server with http will not be synchronous so you will have to change the design of your function to return a promise instead of a synchronous API.
That's because its hosted on a web-server, you need to send a HTTP GET to fetch it locally.
Install the axios package and issue a HTTP GET request to fetch the remote resource from the web-server.
npm install --save axios
Here's a program of the general idea
const fs = require('fs');
const axios = require('axios');
const { promisify } = require('util');
const writeFilePromise = promisify(fs.writeFile);
(async () => {
const url = 'http://techslides.com/demos/sample-videos/small.mp4';
const response = await axios.get(url);
if (response.data) {
await writeFilePromise('small.mp4', response.data);
}
})();
I'm unable to upload an image from my React Native application in Android (iOS works fine). On attempting to upload, I'm receiving the following error:
Error: Network Error
at createError (buildURL.js:33)
at XMLHttpRequest.handleError (xhr.js:144)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:576)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:392)
at XMLHttpRequest.js:505
at RCTDeviceEventEmitter.emit (EventEmitter.js:190)
at MessageQueue.__callFunction (MessageQueue.js:344)
at MessageQueue.js:107
at MessageQueue.__guard (MessageQueue.js:291)
All other requests work, connection is configured to my local static IP address, as I'm testing on a physical device, rather than in a simulator.
I've looked at a number of solutions already:
https://github.com/facebook/react-native/issues/9506
Suggests to add a type filed to data, which I had already (detailed below)
https://github.com/facebook/react-native/issues/10404
Suggests to use IP instead of localhost, which I have been doing since the start
Here is the code that handles this:
ProfileImage.js (wrapper for react-native-image-picker):
async onHandleResizedImageUri(image) {
var data = new FormData();
data.append("profile_image", {
uri: image.path,
name: image.name,
type: "image/jpeg"
});
let imageUploadSuccess = await global.api.callPostImage("/api/profile-image", data);
Api.js
async callPostImage(url, data) {
try {
const settings = {
baseURL: config.api_host,
timeout: 1000,
headers: {
"content-type": "multipart/form-data"
}
};
const response = await this.axios.post(url, data, settings);
return response;
} catch (error) {
console.log(error);
}
}
Also, this issue is happening in both debug and release mode, and for multiple servers (local, staging and production). All other network requests work fine, but this one will not complete. Anyone have any suggestions?
Device information:
Samsung Galaxy A5 (2017)
Model SMA520W
Version 8.0.0