I am working on a review/comment process and would like to be able to store each review/comment given by a user for another one in firebase, with each review/rating given at the same time being under the same timestamp. I was using the JS function new Date().getTime() initially, and this works fine, but to ensure that users can't tamper with the value (ie by changing the date on their computer) I would like to use firebase timestamp instead.
Now, I want the end product to be a map:
[chatStartTime] : {
review : stars,
time : chatStartTime
}
Which I then incorporate into a firebase document using transaction.set(). Now the issue is the ChatStartTime, which is supposed to represent this timestamp. I have the code:
var chatStartTime = firebase.firestore.FieldValue.serverTimestamp();
As an aside, I am also hoping to get:
var chatStartTimeDate = chatStartTime.toDate();
However chatStartTimeDate leads to an error – my understanding is that this is because chatStartTime is essentially "void" until it is added to a document; is there any way to get around this and use this function? Also, if I just use chatStartTime for both instances, I end up with a map on the database of the form:
[object Object]
review : 4.5
time :
.sv: "timestamp"
Where .sv is a string type. How can I solve this issue? I'd like the end result to look something like (for example)
1579194722735
review : 4.5
time : 1579194722735
The FieldValue.serverTimestamp() value is written on the server at time of write. So you can't get it before the write happens. However, you can get the writeTime from the response. This should give you what you're after. Alternately, you could read the document back out after you write it to fetch the timestamp (but that seems unnecessary).
myCollection
.doc('foo')
.create({ timestamp: FirebaseFirestore.FieldValue.serverTimestamp() })
.then(result => {
const serverTime: Date = result.writeTime.toDate();
});
Related
I'm trying to do a swap on Pancakswap using JS and web3. I am using the swapExactTokensForTokensSupportingFeeOnTransferTokens function on the routerV2 contract.
The code in question:
let result = await routerV2Contract.methods.swapExactTokensForTokensSupportingFeeOnTransferTokens(
amountIn, //
amountOutMin, //
[BNB_ADDRESS, BUSD_ADDRESS],
myADDRESS,
DEADLINE); // timestamp in seconds one minute from now
BNB_ADDRESS = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
BUSD_ADDRESS = '0xe9e7cea3dedca5984780bafc599bd69add087d56'
amountIn = 5000000000000000 0.005 BNB
amountOutMin = Calculated based on BNB value at the moment
It does return an object in "result" which contains, among other things, A call, send and method component, however the swap doesn't actually happen, nor is an error of any kind given.
I wonder if I should pass gas information in the "send" component, but I can't find information on what the send function requires.
On another note, actually I realized I connected to web3 with just a provider, but it isn't actually an hdwalletprovider. I guess I can't expect it to work, but I would have expected some kind of error, and I will likely still need to know how to send the gas info and provide any other support the function requires.
Can anyone provide clues as to what more I may need?
Or any code samples that would do a web3 swap on the pancakeswap v2 router..?
I'm new to the javascript world and have been tinkering with Actions on Google. I had an action that was previously running but after attempting to simplify my code, I've been running into a new error I cannot seem to figure out. The code is supposed to make a call to a database and return information to the user based on the date they have selected.
Problem:
The code is supposed to make a call to a database and return information to the user based on the date they have selected. The intent seems to break down when I call the URL using axios. When I test my function I receive the following error from the Google Cloud Platform: "Error: No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?"
app.intent('Moon', (conv, {date}) => {
// Sets date to today if none given
if (!date) {
date = new Date().toISOString();
}
// Slices date string to match date format in database
const dateSlice = date.slice(5, 9);
// Call to row in dabase for the given date
function getData() {
return axios.get(`example.com/search?Date=${dateSlice}`);
}
return getData().then(res => {
res.data.map(con => {
conv.ask(`On X date there will be a ${con.Object1}`);
});
});
});
I don't know much about Promise and await but that seems to be the issue. I'm not sure how I was able to get my code to run before without these objects. I've tried to insert a Promise object before my return but it makes the rest of the function unreachable. I also checked to see if Axios had any updates but it has not, I am on the latest version. Does the error have to do with one of the returns perhaps?
It could be related to Promises, but you seem to be handling them correctly.
The call to axios.get() is returning a Promise...
... which you are returning in getData()...
... which is returned in the 'Moon' Intent handler as part of the getData().then() block.
I suspect more that this is a logic problem. If res.data is an empty array, then there will be no calls to conv.ask(), so you end up not asking anything.
There is also a problem if res.data has more than two items. In this case, you'll generate an error because you've replied with more than two "simple" responses.
Either way - you may wish to log res and/or res.data to make sure you're getting back what you think.
I believe this is a known issue, but just checking... when using javascript and sc.get to do a search, the created_at search filter seems to be completely ignored. Tried it using just a 'from' hash, as well as used both 'from' and 'to' hashes to the created_to variable... nada. I get ALL music regardless of date. I'm using the correct syntax as well. ie: 2018-01-01 00:00:00. Other filters in the call seem to work fine. Not sure if this is a problem with the client side or on your server that the request is being made of just not being able to parse it. I'm going to try including this filter using CURL and see if it works there, but I sure would like it to work in my javascript call instead.
relevant code (other filters are used but have been stripped out to simplify the testing of this):
var qcreated='2018-01-01 00:00:00';
SC.get('/tracks', { created_at: { from: qcreated } }, function (tracks) { ...rest of code here... }
the rest of the code bit is extensive and parses and sorts the results. unfortunately the query seems to ignore the created_at filter and I get back songs of all manner of date regardless of what I hard code in.
According to the documentation, SC.get() returns a promise, it doesn't take the callback as an argument. So it should be:
SC.get("/tracks", { created_at: { from: qcreated } }).then(function (tracks) {
// rest of code here
});
This is for version 3 of the SoundCloud SDK -- your method of calling is appropriate for Version 1.
Thanks for everyone's help, but I have figured it out. You can't use both the created_at and genre filters. There is still a bug in the API which will conflict in priority and ONLY the genres: filter will spawn results and the created_at will be ignored. If you LEAVE OUT genres: filter, the created_at WILL work. The only way both will work at the same time is by use created_at: last_year instead of from: For some reason this works, but is limited to just the last years worth of uploads.
Problem
In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:
firebase.database().ref('posts').child(this.state.passKey).update({
comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})
When I actually run this code, I get an error saying:
Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.
My Server
-
To get the comments of particular post you should do like this
let postId='someId'
postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
});
It looks like you're expecting this bit of code to query the database:
firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
Unfortunately, it doesn't work that way. There's no snapshot property on a database Reference object returned by child() or ref().
Instead, you'll need to query the database at that reference, then when you're called back with its value, you can apply it elsewhere.
var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
// use the snapshot here
})
I'm using firebase's database to host a real time messaging web app. I've finished the basic implementation but now want to add timestamps for users to see when messages were sent.
To add a message to a chatroom I use firebase.push() with the value as the sent message. I guess I'll have to switch this to .update() with the value as an object with the fields message and time sent.
I'm trying to figure out how to populate this time sent. I've heard of firebase.database.ServerValue.TIMESTAMP but from what I can tell it's hard to change that into a date and time. I'm heavily considering a unix timestamp like this var unix = Math.round(+new Date()/1000); since this is easy to convert.
Does anybody with any experience have any tips for which direction I should take? I don't need the timestamp to be super precise, just the date, hour, and minute.
When you write firebase.database.ServerValue.TIMESTAMP to a node, the value becomes a UNIX timestamp. See the Firebase reference documentation for an example how to write this:
var sessionsRef = firebase.database().ref('sessions');
var mySessionRef = sessionsRef.push();
mySessionRef.update({ startedAt: firebase.database.ServerValue.TIMESTAMP });
When you read this value, you can convert it back into a Date object with:
sessionsRef.on('child_added', function(snapshot) {
var startedAt = new Date(snapshot.val().startedAt);
});