back4app cloud code functions problem with ES version - javascript

I'm using back4app as backend service to deploy my app, developed in React Native ans JS. I'm testing how to use the 'Cloud Code Functions' of back4app right now...
As I'm a beginner in back4app, I found a problem while using their guide. I'm facing this message error: 'async functions' is only available in ES8 (use 'esversion: 8').:
Back4app Side:
import Parse from 'parse/react-native.js';
import AsyncStorage from '#react-native-async-storage/async-storage';
//Initializing the SDK
Parse.setAsyncStorage(AsyncStorage);
//Paste below the Back4App Application ID AND the JavaScript KEY
Parse.initialize('YOUR_APPLICATION_ID_HERE', 'YOUR_JAVASCRIPT_KEY_HERE');
//Point to Back4App Parse API address
Parse.serverURL = 'https://parseapi.back4app.com/';
//This is a hello function and will log a message on the console
Parse.Cloud.define("hello", async (request) => {
console.log("Hello from Cloud Code!");
return "Hello from Cloud Code!";
});
my app:
const helloFunction = await Parse.Cloud.run("hello");
I know the problem comes from the asynchronous function, but I can't find any solution. What am I doing wrong?

When using the cloud code, you do not need to do any kind of initialization as it is already done on the Back4app servers.
So in your main.js file please remove:
The import statements. Lines 2 and 3.
The Parse initialization. The lines 6,8, and 10.

To really answer the question, the error message is a false positive! Probably the online editor is not configured right, so it shows these warnings accidentally...
Just as the guide tells you, you can use the async keyword if you got a running 3.x Parse Server. It should be safe to use this, as per default Parse Server 4.x is used (the server can be changed via App Settings > Server Settings > Manage Parse Server).

Related

Firebase Cloud Messaging - getToken() error in JS SDK

I am developing an app for KaiOS, a fork of the old Firefox OS, for which apps are based on HTML5 + JS. When compiling and running on device, Firebase Cloud Messaging stops working. I receive an error as follows.
TypeError: 'applicationServerKey' member of
PushSubscriptionOptionsInit could not be converted to any of:
ArrayBufferView, ArrayBuffer.
I've followed the instructions in the Firebase docs here as well as this tutorial on setting up FCM for Progressive Web Apps here. I can confirm I am setting my key before calling getToken, as shown below:
import firebase from "firebase";
const messaging = firebase.messaging();
messaging.usePublicVapidKey("<my-key-from-firebase-settings>");
messaging.getToken().then((currentToken) => {
...
}).catch((err) => {
...
});
I have searched for the above error, but to no avail. Does anyone know what this error means or how to fix it? Running the application in the browser on my development machine, everything works perfectly.
Note: the app is written in Typescript
Firebase SDK Version: 7.12.0
It might not be of help to you but I recently experienced a similar error message in a different environment:
Error: Failed to execute 'subscribe' on 'PushManager': The provided
value is not of type '(ArrayBuffer or ArrayBufferView)'
It was fixed in version 7.13.2 of Firebase:
https://github.com/firebase/firebase-js-sdk/issues/2712
https://firebase.google.com/support/release-notes/js#version_7132_-_april_2_2020

How to use firestore emulator from client

I want to test locally my firebase functions.
These functions make firestore queries.
So i start the emulator firebase emulators:start and in my client i use firebase.functions().useFunctionsEmulator('http://localhost:5001').
My functions work well when i call them in my client. I can read/write data inside the firestore emulator.
The problem :
I want to read the firestore emulator data directly inside my client, like :
firebase.firestore().collection('tests').get().then(tests => {
console.log( tests.docs.map(test=>test.map) )
})
but i can't find how to set the firestore emulator inside my client.
here what i tried:
1) Firestore setting
firebase.firestore().settings({
host:'http://localhost:8080',
ssl:false
})
result :
i get #firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds. inside my client console.
The http request returns 'Not found'
2) Set the emulator url inside my firebaseConfig
var firebaseConfig = {
// ...
databaseURL: "http://localhost:8080",
// ...
}
firebase.initializeApp(firebaseConfig)
in this case, the remote server (https://firestore.googleapis.com..) is requested.
So i want to setup one of these two cases :
1) Using the remote firestore inside my functions emulators
OR
2) Using the local firestore emulator inside my client code.
Anyone has already done this ?
Install the testing lib
npm i -D #firebase/testing
Setup and start the emulator in another terminal:
firebase setup:emulators:firestore
firebase serve --only firestore
Setup the tests
const firebase = require("#firebase/testing");
// Helper function to setup test db
function authedApp(auth) {
return firebase
.initializeTestApp({ projectId: FIRESTORE_PROJECT_ID, auth })
.firestore();
}
// Setup methods
beforeEach(async () => {
// Clear the database between tests
await firebase.clearFirestoreData({ projectId: FIRESTORE_PROJECT_ID });
});
// Clean up apps between tests.
afterEach(async () => {
await Promise.all(firebase.apps().map(app => app.delete()));
});
Run the tests
it("should retrieve correct item", async () => {
// Init test db
const db = authedApp(null);
// Manually add item to collection
const ref = await db.collection(COLLECTION_NAME).add({name: 'test item'});
// Fetch item by id
const resp = await db.collection(COLLECTION_NAME).doc(ref.id).get();
// test the output
expect(resp).toBeDefined();
expect(resp).toEqual(expect.objectContaining({name: 'test item'}));
});
Of course your particular setup and circumstances will differ, but this at least should give you a general idea. More info: https://firebase.google.com/docs/rules/unit-tests
Note from 'Test your Cloud Firestore Security Rules'
Data written to the Cloud Firestore emulator is held in memory until
the emulator is stopped. If the emulator is run continuously, this may
have an impact on test isolation. To ensure that data written in one
test is not read in another, either explicitly clear your data with
clearFirestoreData, or assign a different project ID for each
independent test: when you call firebase.initializeAdminApp or
firebase.initializeTestApp, append a user ID, timestamp, or random
integer to the projectID.
Edit: I wrote a blog post a while back, which goes into more detail about the subject.
I had the same problem too. I found the following example, which looks like its still work in progress:
https://github.com/firebase/quickstart-nodejs/tree/master/firestore-emulator/browser-quickstart
They didn't use #firebase/testing directly in their example. When I did try to embed #firebase/testing in my webpack code, it tries to connect via grpc, which attempts to do fs.existsSync, which doesn't exist in webpack browser context. They prefer to enable the functionality via WebChannel instead.
Some caveats as of Nov 2019:
You might see errors in the console connecting to localhost:8080 with the X-Goog-API. I am not sure what is that for.
You might get the following error: #firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
Despite having those errors, my functions were still able to connect to the local firestore.
Versions I had at time I was testing:
firebase-js-sdk 6.3.1
firebase-tools 7.3.1
Update 11/18:
I raised an issue within quickstart-nodejs github and it seems I just needed to use the latest versions of everything.
Versions:
firebase-js-sdk v7.4.0
firebase-tools v7.8.0
Ok i found how to do it :
1) launch the functions emulator locally:
set GOOGLE_APPLICATION_CREDENTIALS=./privatekey.json && firebase serve --only functions
2) then client side:
if (process.env.NODE_ENV === 'development') {
firebase.functions().useFunctionsEmulator('http://localhost:5001')
}
Okay, this is trivial... In your firestore cient config you should have provided the host, not the origin for firestore (the protocol is set using the ssl parameter):
firebase.firestore().settings({
host: 'localhost:8080',
ssl: false
})
At least this solved it for me when I had the exact same error.
Just FYI to anyone who's reading this - if you run into problems with the firestore client, you can use debug level logging, just set firebase.firestore.setLogLevel('debug'). Had the OP done that, he might have noticed that firebase is accessing firestore at http://http://localhost:8080/...
Define the FIRESTORE_EMULATOR_HOST environment variable
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
export FIRESTORE_EMULATOR_HOST=localhost:8080
or just add FIRESTORE_EMULATOR_HOST=localhost:8080 to your .env file

Feathers JS Client Timeout

I am sure this is something very simple, but for some reason I am getting a timeout when trying to integrate the feathersjs client with a very simple jQuery app. Files of interest are in src below. This repo only contains a single service "messages" connected to a NedB database with no authentication. When the script starts, I am attempting to add a single message to my messages service.
Repo:
https://github.com/Ryan8765/jquery-chat
Error in Console:
Uncaught (in promise) Error: Timeout of 5000ms exceeded calling create on messages
at client.js:66
The server side application has been created with an old (v2) version of the CLI but you are loading #feathersjs/feathers#^3.0.0 in the browser which uses a Socket.io message format that is not supported by a v2 server.
feathers --version on the command line should show 3.3.0 or later. You can either follow the migration guide to upgrade or install the latest #feathersjs/cli and regenerate the application.
Reference issue feathersjs/feathers#761.
I had the same issue using these package's on the client side with vue.js (vue#^3.2.16 and vite#2.6.4)
#feathersjs/feathers#^4.5.11
#feathersjs/socketio-client#^4.5.11
socket.io-client#^2.3.1
#feathersjs/authentication-client#^4.5.11
My issue was resolved after changing
import io from "socket.io-client";
to
import io from "socket.io-client/dist/socket.io";
in my client feathersjs settings

Meteor package working on browser but not on server

There's no problem using the Meteor package strikeout:string.js on the client side (browser JS console), but it throws an error when using it on the server side.
Checked package.js and found api.addFiles('lib/string.js', ['client','server']);, is this not sufficient?
Test code
console.log(S('jon').capitalize().s)
Error on server
ReferenceError: S is not defined
is this not sufficient? YES, you are getting the Reference because you are not requiring it.
In order to use it on the server, you should require it, on this example im using meteorhacks:npm.
It was not possible for me create a Meteorpad of this, so i will do here step-by-step.
First meteor add meteor hacks:npm
Second On the recent create packages.json add this line
{
"string": "3.1.0"
}
Third Now just add the server code.
if (Meteor.isServer) {
Meteor.startup(function () {
var S = Meteor.npmRequire('string'); //server side
console.log(S('jon').capitalize().s)
});
}
Expected Output
I20150326-10:54:05.639(-5)? Jon
Hope it works for you.

Meteor 1.0 MiniMongo not serving objects to the client side

I am working through the Discover Meteor tutorial, and even though the Posts = new Mongo.Collection('posts'); has all of its functions working on the server side Mongo shell, calling the Posts functions on the browser console is simply not working:
Posts.insert()
ReferenceError: Posts is not defined`
The collection is declared in my lib/collections folder in a posts.js file, as such:
Posts = new Mongo.Collection('posts');
if (Meteor.isServer) {
Meteor.publish('posts', function(){
return Posts.find()});
}
if(Meteor.isClient) {
Meteor.subscribe('posts');
}
`
Any ideas or suggestions? When I run db.posts.insert({title: "postname}) in the Mongo shell, the new post shows up instantly asynchronously in the browser, so I know the DB is functioning.
I'm pretty early on in the tutorial so I feel like this shouldn't be happening.
First( just to good practices), on console run this
Cd myApp
meteor remove autopublish
Now you need to Publish (server side), Subscribe(client side), all yours Collections
//server side
Posts = new Mongo.Collection('Posts');
Meteor.publish('Posts', function(){
return Posts.find()
});
/Client side
Posts = new Mongo.Collection('Posts');
Meteor.subscribe('Posts');
Hope This Works mate, and keep discovering Meteor
Are you using a web based cloud IDE?
If yes...
Do not use the javascript console that comes with your web based IDE. Run your output in a real browser like firefox or chrome and use their javascript console.

Categories