I am trying to retrieve data (Image) from the Google Spreadsheet in Javascript:
Image of the Error:
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: SPREADSHEET_ID,
range: 'Sheet1',
includeValuesInResponse: true,
}, function(err, result) {
if(err) {
// Handle error.
console.log(err);
} else {
console.log('%d cells appended.', JSON.stringify(result, null, 2));
}
});
I am receiving an error on the code.
You want to retrieve hyperlinks from "Sheet1" of a Spreadsheet. If my understanding is correct, how about this modification?
Modification point:
You can retrieve the hyperlinks from a sheet using sheets.spreadsheets.get. In this case, sheets/data/rowData/values/hyperlink is used as the fields.
Modified script:
sheets.spreadsheets.get({
auth: jwtClient,
spreadsheetId: SPREADSHEET_ID,
ranges: 'Sheet1',
fields: "sheets/data/rowData/values/hyperlink",
}, function(err, result) {
if (err) {
// Handle error.
console.log(err);
} else {
console.log('%d cells appended.', JSON.stringify(result, null, 2));
}
});
Result:
When no error occurs, the object like below is returned.
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"hyperlink": "https://sample.com/img1.jpg"
}
]
},
{
"values": [
{
"hyperlink": "https://sample.com/img2.jpg"
}
]
},
{},
{},
{},
]
}
]
}
]
}
Note:
This modified script supposes that you can use Sheets API.
I couldn't understand about console.log('%d cells appended.', JSON.stringify(result, null, 2));. If the error occurs at this line, please try the following script.
console.log(JSON.stringify(result.data, null, 2));
Reference:
spreadsheets.get
If I misunderstand your question, I'm sorry.
I figure it out:
async.series([
function makeAnAuthorizedApiCall(callback){
var {google} = require('googleapis');
var sheets = google.sheets('v4');
const storage = new Storage({ projectId: PROJECT_ID });
const jsonCredentialsFile = storage.bucket(BUCKET_NAME).file(SERVICE_ACCT_JSON_FILE);
retrieveFromGCStorage(jsonCredentialsFile).then(creds => {
console.log("Credentials successfully obtained from Google Cloud Storage");
var jwtClient = new google.auth.JWT(
creds.client_email,
null,
creds.private_key,
API_SCOPES, // an array of auth scopes
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
});
Related
Below is the piece of code i have written , to get the result but null in response
I am using selectObjectContent api to get the results with the simple SQL query
const bucket = 'myBucketname'
const key = 'file.json.gz'
const query = "SELECT * FROM s3object "
const params = {
Bucket: bucket,
Key: key,
ExpressionType: "SQL",
Expression: query,
InputSerialization: {
CompressionType: "GZIP",
JSON: {
Type: "LINES"
},
},
OutputSerialization: {
JSON: {
RecordDelimiter: ","
}
}
}
s3.selectObjectContent(params,(err, data) => {
if (err) {
console.log(data)
} else {
console.log(err)
}
})
I have found the solution to it. was logging error when getting successfull result/data , so corrected it below. Also i have found the way to read stream buffer data
s3.selectObjectContent(params,(err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
})
const eventStream = data.Payload;
// Read events as they are available
eventStream.on('data', (event) => {
if (event.Records) {
// event.Records.Payload is a buffer containing
// a single record, partial records, or multiple records
var records = event.Records.Payload.toString();
console.log( records )
} else if (event.Stats) {
console.log(`Processed ${event.Stats.Details.BytesProcessed} bytes`);
} else if (event.End) {
console.log('SelectObjectContent completed');
}
I am developing a full stack application. And my front-end development is ready. I am using vuejs to display array on screen. And now I need to get that array from sql server using nodejs. I really dont know about asynchronous functions, http requests and tons of server based knowledges. Just wanted to get a array without any knowledge exploring.
It is my node module which is set for get data or insert data into mssql server.
var dbConfig = {
server: "localhost\\MSSQLSERVER",
database: "sample",
user: 'sa',
password:'deegii2001060108',
port: 1433
};
var result;
exports.dbcontext=function(engine, config){
const sql = require('mssql')
return new Promise(function(resolve, reject) {
var conn = sql.connect(config, function(err) {
if(err) {
reject(err);
} else {
sql.query(engine, function(err, recordsets,returnValue, affected) {
if(err) {
reject(error);
} else {
result = recordsets;
resolve(recordsets);
conn.close();
}
})
}
})
})
}
exports.dbcontext('select * from tblGender', dbConfig).then(recordset=>{console.log(recordset)});
console.log('result',result);
in console:
result undefined
{
recordsets: [ [ [Object], [Object], [Object], [Object], [Object] ] ],
recordset:[
{ id: 1, gender: 'male' },
{ id: 2, gender: 'female' },
{ id: 3, gender: 'unknown' },
{ id: 4, gender: 'aertq' },
{ id: 5, gender: 'from vscode' } ],
output: {},
rowsAffected: [ 5 ] } //i wanted to store recordset into my global result var outside of function
}
exports.dbcontext(... //connection function
result = recordsets; /*the result variable is not changed but i want to
store recordsets in global variable outside of the function*/
...}
//Unexpected result:
console.log(result) // undefined
//Expected result:
console.log(result) // an array consists of objects
var express = require('express');
var app = express();
var sql = require("mssql");
// config for your database
var config = {
user: 'sa',
password: 'deegii2001060108',
server: 'localhost\\MSSQLSERVER',
database: 'sample'
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from tblGender', function (err, result) {
if (err) console.log(err)
// send records as a response
console.log(JSON.stringify(result));
});
});
var mysql = require('mysql');
var result;
var con = mysql.createConnection({
host: "localhost\\MSSQLSERVER",
user: "sa",
password: "deegii2001060108",
database: "sample"
});
con.connect(function(err) {
if (err) throw err;
con.query("select * from tblGender", function (err, data, fields) {
if (err) throw err;
result = data;
});
console.log(json.stringify(result));
});
I have done a lot of research and the topic does not have enough source for juniors like me. Everything I could find was case specific that was making it impossible to understand. Therefore for myself and for the people who will read this in the future I will not make my question too case specific.
I have created a table record on DynamoDB with the following lambda function:
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'})
exports.handler = (event, context, callback) => {
console.log(event)
const params = {
Item: {
"UserId": {
S: "global"
},
"search": {
SS: [
"" + event.hashtag
]
}
},
TableName: "crypto-app"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
callback(err)
} else {
callback(null, data)
}
});
};
this is creating a simple string set
{
"search": {
"SS": [
"london"
]
},
"UserId": {
"S": "global"
}
}
how can I add more strings to my string set with a lambda function to make it like this?
{
"search": {
"SS": [
"london", "tokyo", "moskow"
]
},
"UserId": {
"S": "global"
}
}
You can update the item and add additional string set values.
Here's how you would do it if you had named the attribute xxx rather than search, which is a reserved word.
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2'});
const params = {
Key: {
UserId: {
S: 'global',
},
},
UpdateExpression: 'ADD xxx :avals',
ExpressionAttributeValues: {
':avals': {
SS: ['tokyo', 'moskow'],
},
},
TableName: 'crypto-app',
};
dynamodb.updateItem(params, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
However, because you named the attribute search, which is reserved, you need to essentially escape that reserved name using an expression attribute name, which is a placeholder that you use in an expression, as an alternative to an actual attribute name.
Here's an example of how you do that:
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2'});
const params = {
Key: {
UserId: {
S: 'global',
},
},
UpdateExpression: 'ADD #a :avals',
ExpressionAttributeValues: {
':avals': {
SS: ['tokyo', 'moskow'],
},
},
ExpressionAttributeNames: {
'#a': 'search',
},
TableName: 'crypto-app',
};
dynamodb.updateItem(paramse, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
Another, probably better, way to do this is to use the DynamoDB DocumentClient. It's a higher level client interface and it simplifies working with items by abstracting away the notion of attribute values, and instead using native JavaScript types.
With the DocumentClient, rather than explicitly writing UserId: { 'S': 'global' }, you can simply use UserId: 'global' and the string type ('S') will be inferred.
Here's an example of the item update using DocumentClient:
const AWS = require('aws-sdk');
const dc = new AWS.DynamoDB.DocumentClient({region: 'us-east-2'});
const params = {
Key: {
UserId: 'global',
},
UpdateExpression: 'ADD #a :avals',
ExpressionAttributeValues: {
':avals': dc.createSet(['tokyo', 'moskow']),
},
ExpressionAttributeNames: {
'#a': 'search',
},
TableName: 'crypto-app',
};
dc.update(params, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
I have a big issue with my function in sails.js (v12). I'm trying to get all userDetail using async (v2.3) for deep populating my user info:
UserController.js:
userDetail: function (req, res) {
var currentUserID = authToken.getUserIDFromToken(req);
async.auto({
//Find the User
user: function (cb) {
User
.findOne({ id: req.params.id })
.populate('userFollowing')
.populate('userFollower')
.populate('trips', { sort: 'createdAt DESC' })
.exec(function (err, foundedUser) {
if (err) {
return res.negotiate(err);
}
if (!foundedUser) {
return res.badRequest();
}
// console.log('foundedUser :', foundedUser);
cb(null, foundedUser);
});
},
//Find me
me: function (cb) {
User
.findOne({ id: currentUserID })
.populate('myLikedTrips')
.populate('userFollowing')
.exec(function (err, user) {
var likedTripIDs = _.pluck(user.myLikedTrips, 'id');
var followingUserIDs = _.pluck(user.userFollowing, 'id');
cb(null, { likedTripIDs, followingUserIDs });
});
},
populatedTrip: ['user', function (results, cb) {
Trip.find({ id: _.pluck(results.user.trips, 'id') })
.populate('comments')
.populate('likes')
.exec(function (err, tripsResults) {
if (err) {
return res.negotiate(err);
}
if (!tripsResults) {
return res.badRequest();
}
cb(null, _.indexBy(tripsResults, 'id'));
});
}],
isLiked: ['populatedTrip', 'me', 'user', function (results, cb) {
var me = results.me;
async.map(results.user.trips, function (trip, callback) {
trip = results.populatedTrip[trip.id];
if (_.contains(me.likedTripIDs, trip.id)) {
trip.hasLiked = true;
} else {
trip.hasLiked = false;
}
callback(null, trip);
}, function (err, isLikedTrip) {
if (err) {
return res.negotiate(err);
}
cb(null, isLikedTrip);
});
}]
},
function finish(err, data) {
if (err) {
console.log('err = ', err);
return res.serverError(err);
}
var userFinal = data.user;
//userFinal.trips = data.isLiked;
userFinal.trips = "test";
return res.json(userFinal);
}
);
},
I tried almost everthing to get this fix but nothing is working...
I am able to get my array of trips(data.isLiked) but I couldn't get my userFInal trips.
I try to set string value on the userFinal.trips:
JSON response
{
"trips": [], // <-- my pb is here !!
"userFollower": [
{
"user": "5777fce1eeef472a1d69bafb",
"follower": "57e44a8997974abc646b29ca",
"id": "57efa5cf605b94666aca0f11"
}
],
"userFollowing": [
{
"user": "57e44a8997974abc646b29ca",
"follower": "5777fce1eeef472a1d69bafb",
"id": "5882099b9c0c9543706d74f6"
}
],
"email": "test2#test.com",
"userName": "dany",
"isPrivate": false,
"bio": "Hello",
"id": "5777fce1eeef472a1d69bafb"
}
Question
How should I do to get my array of trips (isLiked) paste to my user trips array?
Why my results is not what I'm expecting to have?
Thank you for your answers.
Use .toJSON() before overwriting any association in model.
Otherwise default toJSON implementation overrides any changes made to model associated data.
var userFinal = data.user.toJSON(); // Use of toJSON
userFinal.trips = data.isLiked;
return res.json(userFinal);
On another note, use JS .map or _.map in place of async.map as there is not asynchronous operation in inside function. Otherwise you may face RangeError: Maximum call stack size exceeded issue.
Also, it might be better to return any response from final callback only. (Remove res.negotiate, res.badRequest from async.auto's first argument). It allows to make response method terminal
I have the following code I am trying to upload to DynamoDB local host using Node.js.
Is there a possible work around. For the following error?
Unable to add event undefined . Error JSON: {
"message": "One of the required keys was not given a value",
"code": "ValidationException",
"time": "2016-06-28T04:02:26.250Z",
"requestId": "970984e4-3546-41f0-95f9-6f1b7167c510",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
Here is the code. I would like the Item: {} to accept whatever values may be present, and add them to the table.
var AWS = require("aws-sdk");
var fs = require('fs');
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Importing movies into DynamoDB. Please wait.");
var allMovies = JSON.parse(fs.readFileSync('moviedata.json', 'utf8'));
allMovies.forEach(function(movie) {
var params = {
TableName: "Movies",
Item: {
"year": movie.year,
"title": movie.title,
"info": movie.info,
"twitter": movie.twitter
}
};
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add movie", movie.title, ". Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("PutItem succeeded:", movie.title);
}
});
});
As you are looping over a promise call, you need a safeguard that the current promise resolves before you begin the next.
var AWS = require("aws-sdk");
var fs = require('fs');
const tableName = 'Movies';
AWS.config.update({
region: "local",
endpoint: "http://localhost:8000"
});
var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Importing movies into DynamoDB. Please wait.");
var allMovies = JSON.parse(fs.readFileSync('moviedata.json', 'utf8'));
for (let i = 0, p = Promise.resolve(); i < allMovies.length; i++) {
p = p.then(_ => new Promise(resolve =>
setTimeout(function () {
var params = {
TableName: tableName,
Item: {
"year": allMovies[i].year,
"title": allMovies[i].title,
"info": allMovies[i].info
}
};
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add movie", allMovies[i].title, ". Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("PutItem succeeded:", allMovies[i].title);
}
});
resolve();
}, 10)
));
}