I am writing a Javascript web app using Firebase. If I have data like this in my JSON tree:
users
session1
screenname:Bill
session2
screenname:Steve
...and I know the session number, how can I grab the "name" of the user? I have tried this:
valUsers.child('session1').child('screenname').once('value', function(data){
console.log(data);
});
But that does not seem to work. I'm guessing I have a syntax issue?
You were pretty close, but needed to use DataSnapshot.val() as shown below to access the point you want.
var ref = new Firebase('yourfirebaselocation/users');
var sessionNum = 'session1';
ref.child(sessionNum + '/screenname').once('value', function(dataSnapshot) {
var specificScreenname = specificPoint.val();
}
When working with Firebase, you may want to also explore working with Promise objects. This would allow you to create some repeatable functions that can reuse your reference on multiple locations and also give Firebase time to respond to your query.
var ref = new Firebase('yourfirebaselocation/users');
function getUser(theRef, location) {
return new Promise(
function(resolve, reject) {
theRef.child(location).once('value', function(dataSnapshot) {
resolve(dataSnapshot);
});
});
}
getUser(ref, specificlocationvariable).then( function(val) {
specificScreenname = val.val();
});
You don't specify other than 'JavaScript' so do keep in mind that Promises are an ECMAScript 6 proposal with some compatibility issues, but a lot of the JS frameworks have an implementation for it that smooth that issue (Angular, Ember, etc).
Related
I am creating an application and at the moment I am trying to get all product data from the store, but the function does not work for me.
I am making a request for this feature, but in the response I have no 'link' at all. And there is no other replacement that would link to the next page.
function makeRequest(
nextLink = 'https://myshopname.myshopify.com/admin/api/2021-04/products.json?limit=1'
) {
return new Promise((resolve, reject) => {
console.log(nextLink);
fetch(nextLink).then((r) => {
const headerLink = r.headers.get('link');
console.log(r.headers);
console.log(headerLink);
const match = headerLink.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
const nextLink = match ? match[1] : false;
if (nextLink) {
makeRequest(nextLink);
} else {
resolve();
}
});
});
}
Can you help me figure out how I can make this function work and how can I get all the data from this store?
If someone were looking for an answer to the same question, the answer turned out to be very simple. For this feature to really work, you need a Shopify Plus store subscription. If you have it, but the code still does not work, then you need to contact Shopify technical support and write to them about this problem.
If you need to get all the products from the store, then I recommend using another way to get data, namely through GraphiQL.
I saw the MS Office js api 1.3 document about custom properties.
But I can not read any custom property item from word settings by office js.
`Word.run(function (context) {
// Create a proxy object for the document.
var thisDocument = context.document;
var customProperties = thisDocument.properties.customProperties;
context.load(customProperties);
return context.sync().then(function () {
var getcount = customProperties.getCount();
console.log(customProperties.items);
return context.sync().then(function () {
console.log(getcount.value);
});
});
})`
The customProperties.items alway return empty array. I also can not find the set method in customProperties
My custom property is show in this (https://i.stack.imgur.com/AywDo.png).
Does MS Office js api not support to access custom properties in word yet?
CallOfDuty: I think what's happening is that you don't have an updated version of your Office Client (you need 16/0.7766+). I ran your code in a recent build and I am getting the custom properties using your exact same code. So just please make sure you are working on a fresh update, here are some instructions on how to do it.
Btw, I just got a simplified version of your code. Hope this helps!
function getProperties() {
Word.run(function (context) {
var customDocProps = context.document.properties.customProperties;
context.load(customDocProps);
return context.sync()
.then(function () {
console.log(customDocProps.items.length);
})
})
}
I am trying to add an object to a PFRelation in Cloud Code. I'm not too comfortable with JS but after a few hours, I've thrown in the towel.
var relation = user.relation("habits");
relation.add(newHabit);
user.save().then(function(success) {
response.success("success!");
});
I made sure that user and habit are valid objects so that isn't the issue. Also, since I am editing a PFUser, I am using the masterkey:
Parse.Cloud.useMasterKey();
Don't throw in the towel yet. The likely cause is hinted at by the variable name newHabit. If it's really new, that's the problem. Objects being saved to relations have to have once been saved themselves. They cannot be new.
So...
var user = // got the user somehow
var newHabit = // create the new habit
// save it, and use promises to keep the code organized
newHabit.save().then(function() {
// newHabit is no longer new, so maybe that wasn't a great variable name
var relation = user.relation("habits");
relation.add(newHabit);
return user.save();
}).then(function(success) {
response.success(success);
}, function(error) {
// you would have had a good hint if this line was here
response.error(error);
});
I've been working on a small Spotify app for some time now. I started using the old API (0.x) but now that I want to access the user's playlists, I need to use the library module which is only available throught the API version 1.0
The spotify team even gives a migration guide to do so ! (if you read me: Thanks guys for all this ;) ).
I already have created a few objects (It's a small app so I don't need much more than that), with a function for each one of my needs, like so:
var sp = getSpotifyApi();
var models = require('sp://import/scripts/api/models');
var player = models.player;
var views = require('sp://import/scripts/api/views');
// in file 'mySpotify.js'
var mySpotify =
{
playerNextTrack: function()
{
player.next();
},
}
Whenever I need to Skip the current track, I can call mySpotify.playerNextTrack();
But now, with the new API, I need to do things like this (from Spotify doc):
require(['$api/models'], function(models) {
var player = models.player;
player.next();
});
My question is simple: how can I include this kind of code into my objects ? How can I give a name to this last "function" ?
As I'm fresh new to JS, I'm probably doing something wrong or understood something the wrong way so feel free to elaborate if you can ;)
I'll start with what I think is the easy answer, just put the require([], function {[all your code]}); around your whole myspotify.js file. That's basically what I have for mine, though I haven't built any of my own objects. Don't think that would matter.
Update
Here's a bit more info showing that objects can be used. Is this what you were looking for?
require(['$api/models'], function(models) {
var mySpotify =
{
playerNextTrack: function()
{
models.player.skipToNextTrack();
},
}
var playtest = document.querySelector('#playtest');
playtest.addEventListener('click', function() { mySpotify.playerNextTrack(); });
});
I am trying to create a webapp on a node/couchdb/windows stack but get terribly stung by what seems to be a lack of experience.
In the database, there is a view that returns all users with passwords. Based on the tutorial for a blog I have tried to access the view through my node code.
Whenever I investigate the structure of the users or users variable, I get an undefined object.
The call to getDatabase() has been tested elsewhere and works at least for creating new documents.
function GetUser(login)
{
var users = GetUsers();
return users[login];
}
function GetUsers() {
var db = getDatabase();
var usersByEmail = [];
db.view("accounts", "password_by_email")
.then(function (resp) {
resp.rows.forEach(function (x) { usersByEmail[x.key] = x.value});
});
//usersByEmail['test'] = 'test';
return usersByEmail;
}
I am aware that both the use of non-hashed passwords as well as reading all users from the database is prohibitive in the final product - just in case anyone wanted to comment on that.
In case something is wrong with the way I access the view: I am using a design document called '_design/accounts' with the view name 'password_by_email'.
Your call to db.view is asynchronous, so when you hit return usersByEmail the object hasn't yet been populated. You simply can't return values from async code; you need to have it make a callback that will execute the code that relies on the result.