Is it possible to execute a string as a query in meteor?
If I have a collection named SampleCollection
const collectionName = 'SampleCollection';
const _query = collectionName+'.find()';
How can i execute _query?
Thanks in advance.
Sure, try this:
const collectionName = 'SampleCollection';
const _query = collectionName+'.find()';
var func = new Function(_query);
func();
Related
I'm streaming a collection from firebase and I want to add the auto generated ID into ProductModel object.
Stream<List<ProductModel>> getprod() {
return _firestore
.collection('macarons')
.snapshots()
.map((QuerySnapshot query) {
List<ProductModel> retVal = [];
query.docs.forEach((element) {
retVal.add(ProductModel.fromJson(element.data(), element.id));
print(element.id);
});
return retVal;
});
}
This is my model
ProductModel.fromJson(
Map<String, dynamic> prod,
String id,
) {
id = id;
description = prod['description'] ?? 'Classic macaron';
flavor = prod['flavor'] ?? 'Choclate';
name = prod["name"] ?? 'empty';
price = prod["price"] ?? 5;
imageurl = prod["imageurl"] ?? 'www.google.com';
}
I do get the ID printed out however it is not rendering on my widget. I get the 'data != null' assertion.
Your problem is in:
id = id;
The left-hand side of the expression is also interpreted as the id parameter, so the assignment becomes a noop.
You'll want to do this instead:
this.id = id;
I am new to flutter and firebase,I want to upload PDF into firebase using flutter please help me to solve this by providing some resources or links..
Thanks in advance.
You can use this dependency https://pub.dev/packages/file_picker to upload any kind of file to database.
final mainReference = FirebaseDatabase.instance.reference().child('Database');
Future getPdfAndUpload()async{
var rng = new Random();
String randomName="";
for (var i = 0; i < 20; i++) {
print(rng.nextInt(100));
randomName += rng.nextInt(100).toString();
}
File file = await FilePicker.getFile(type: FileType.CUSTOM, fileExtension: 'pdf');
String fileName = '${randomName}.pdf';
print(fileName);
print('${file.readAsBytesSync()}');
savePdf(file.readAsBytesSync(), fileName);
}
Future savePdf(List<int> asset, String name) async {
StorageReference reference = FirebaseStorage.instance.ref().child(name);
StorageUploadTask uploadTask = reference.putData(asset);
String url = await (await uploadTask.onComplete).ref.getDownloadURL();
print(url);
documentFileUpload(url);
return url;
}
void documentFileUpload(String str) {
var data = {
"PDF": str,
};
mainReference.child("Documents").child('pdf').set(data).then((v) {
});
}
By this you can upload file to firebase storage and insert the url to realtime database.
I am using andpor/react-native-sqlite-storage for managing SQLite database in React native. Now I want to backup all my database into a encrypted file then restore this at later time. I know how to do this in android but don't know how to implement this in react native.
if it is in android below code can help me to backup the database without encryption.
final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
So my question is how I can do this on React Native. If there any libraries available please suggest to me.
Same approach can be followed in react-native too using react-native-fs package.
import RNFS from 'react-native-fs';
RNFS.readFile("/data/data/<your.app.package>/databases/foo.db", "base64")
.then(value => RNFS
.getAllExternalFilesDirs()
.then(path => RNFS.writeFile(path + "/" + "database_copy.db", value, "base64"))
.then(() => console.log("Successful"))
)
Here is what I did:
import DocPicker from 'react-native-document-picker'
import RNFS from 'react-native-fs'
async function restoreDB() {
try {
const rsPicker = await DocPicker.pickSingle()
const filePath = rsPicker.uri
await RNFS.copyFile(filePath, '/data/data/<your.app.package>/databases/foo.db')
msgInfo('Success Restore Database');
} catch (e) {
msgError(e)
}
}
async function backupDB() {
try {
const currDate = format(new Date(), 'dd_MM_yyyy_HHmmss')
const destPath = RNFS.ExternalStorageDirectoryPath + 'foo_' + currDate + '.db'
await RNFS.copyFile('/data/data/<your.app.package>/databases/foo.db',
destPath)
msgInfo('Success Backup Database');
} catch (e) {
msgError(e)
}
}
able to get function.database.ref to work for basic chains like this.
functions.database.ref("/following/{uid}/{followingId}").onCreate(event => {
var uid = event.params.uid;
var fromId = event.params.fromId
however I have no idea what to do when we are creating something with an autoId that has a sub branch in this case fromId.
exports.NewActMessage = functions.database.ref("/ActPosts/{uid}/messages/autoId/{fromId}").onCreate(event => {
var uid = event.params.uid; //JOSIAH SAVINO
var fromId = event.params.fromId
Whats even more challenging is the autoId is what is being created but I need to pull the "fromId" information from the branch inside of the autoId.
image
Firebase messaged me how to get first the autoId than the fromId from that like so...
exports.NewActMessage =
functions.database.ref('/ActPosts/{uid}/messages/{autoId}').onCreate(event => {
var uid = event.params.uid;
var autoId = event.params.autoId;
var fromId = event.data.val().fromId;
I have followed the instructions here http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ - to connect to the SQLite database synchronously.
public SQLiteConnection GetConnection()
{
var dbFilname = "localDB.db3";
string docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(docsPath, dbFilname);
var plat = new SQLitePlatformAndroid();
var conn = new SQLiteConnection(plat, path);
return conn;
}
I want to change it to an asynchronous connection (SQLiteAsyncConnection) but can't get it to work.
According to the instructions here - https://components.xamarin.com/gettingstarted/sqlite-net -it just needs the path as a parameter
var conn = new SQLiteAsyncConnection(path);
that doesn't work, the error says that the parameters expected are:
a connection function, a TaskScheduler and TaskCreationOptions
I have no idea what to do and have not been able to find any examples that work.
Thanks in advance
You could simply reuse the GetConnection method you already have and create async connection like this:
var asyncDb = new SQLiteAsyncConnection(() => GetConnection());
Xamarin Platforms Android Config, iOS and WP basicaly equal
public SQLiteAsyncConnection GetConnectionAsync()
{
const string fileName = "MyDB.db3";
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, fileName);
var platform = new SQLitePlatformAndroid();
var param = new SQLiteConnectionString(path, false);
var connection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(platform, param));
return connection;
}