Error while i am trying to deploy the function - javascript

I am trying to deploy my code but I am getting error
My index code is:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
console.log('A notification has been deleted :' , notification_id);
return null;
}
const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');
return DeviceToken.then(result =>
{
const token_id = result.val();
const payload =
{
notification:
{
title: "New Chat Request",
body: `you have a new Chat Request, Please Check.`,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was a notification feature.');
});
});
});
Error shown is this:
npm ERR! code ELIFECYCLE npm ERR! errno 1
npm ERR! functions# lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions# lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Admin\AppData\Roaming\npm-cache\_logs\2020-06-02T11_48_47_185Z-debug.log
events.js:287
throw er; // Unhandled 'error' event
^
}
Error: functions predeploy error: Command terminated with non-zero exit code1
The error shown in C:\Users\Admin\AppData\Roaming\npm-cache_logs\2020-06-02T11_48_47_185Z-debug.log :
error code ELIFECYCLE
error errno 1
error functions# lint: `eslint .`
error Exit status 1
error Failed at the functions# lint script.
error This is probably not a problem with npm. There is likely additional logging output above.
verbose exit [ 1, true ]

You need the catch the error here:
then(response =>
{
console.log('This was a notification feature.');
}).catch(error => handle error here)

Related

Why does the yarn command report an error when executing my script

const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const root = __dirname;
function createSpawn(command, next) {
const child = spawn(command, {
stdio: 'inherit',
shell: true,
cwd: root,
});
child.on('exit', function () {
if (typeof next === 'function') {
next();
}
});
}
function install() {
fs.rmSync(path.resolve(root, '.husky'), {
recursive: true,
force: true,
});
createSpawn('npx husky install', () => {
createSpawn('npx husky add .husky/pre-commit "npm run pre-commit"', () => {
createSpawn('npx husky add .husky/commit-msg "npx --no -- commitlint --edit #EE#"', () => {
const file = path.resolve(root, '.husky/commit-msg');
let content = fs.readFileSync(file).toString();
content = content.replace('#EE#', '"$1"');
fs.writeFileSync(file, content);
});
});
});
}
install();
I test on windows, and it work fine
I used the yarn command to install dependencies, but an error occurred. It was still good before. I suspect that the permissions of the husky file are incorrect, but an error is reported in the yarn script
but when i use the commnad node install.js it work fine!!!
here is the error
yarn install v1.22.19
[1/4] 🔍 Resolving packages...
success Already up-to-date.
$ node install.js
node:internal/fs/utils:344
throw err;
^
Error: ENOENT: no such file or directory, open '/Users/ao/Desktop/Yeastar/PPV3/ppfe/.husky/commit-msg'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at /Users/ao/Desktop/Yeastar/PPV3/ppfe/install.js:32:34
at ChildProcess.<anonymous> (/Users/ao/Desktop/Yeastar/PPV3/ppfe/install.js:17:13)
at ChildProcess.emit (node:events:526:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/Users/ao/Desktop/Yeastar/PPV3/ppfe/.husky/commit-msg'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

classifier.__proto__ = LogisticRegressionClassifier.prototype

Creating an endpoint which respond with array of classifications based several ML models based on NaturalJS. I have two questions:
how to resolve this err,
how to force it to be sync.
The err and console.log:
Example app listening at http://localhost:3000
./src/data/
[ './src/data/trained.json', './src/data/untrained.json' ]
./src/data/trained.json
./src/data/untrained.json
[]
/Users/xyz/Desktop/Classifier/classifai-master/node_modules/apparatus/lib/apparatus/classifier/logistic_regression_classifier.js:178
classifier.__proto__ = LogisticRegressionClassifier.prototype;
^
TypeError: Cannot set property '__proto__' of undefined
at Function.restore (/Users/xyz/Desktop/Classifier/classifai-master/node_modules/apparatus/lib/apparatus/classifier/logistic_regression_classifier.js:178:26)
at restore (/Users/xyz/Desktop/Classifier/classifai-master/node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js:36:67)
at /Users/xyz/Desktop/Classifier/classifai-master/node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js:47:27
at /Users/xyz/Desktop/Classifier/classifai-master/node_modules/natural/lib/natural/classifiers/classifier.js:436:13
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! compoclassi#1.0.0 start: `node ./src/server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the compoclassi#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/zyx/.npm/_logs/2021-01-08T12_02_36_696Z-debug.log
Please also see the code endpoint,
app.post('/suggest', urlencodedParser, (req, res) => {
if (req.body.description) {
var classifications = [];
const modelsPaths = readDirectory('./src/data/')
console.log(modelsPaths)
modelsPaths.forEach(modelsPath => {
console.log(modelsPath)
natural.LogisticRegressionClassifier.load(modelsPath, null, function (err, classifier) {
if (err) {
console.log(err)
} else {
var classification = {};
classification['path'] = modelsPath;
classification['classification'] = classifier.classify(req.body.description);
classifications.push(classification);
console.log(classifications)
}
})
})
console.log(classifications)
} else {
res.render('./pages/main')
}
})
And below the readDirectory() function: (but this seems work fine)
function readDirectory(directory) {
console.log(directory)
const tree = dirTree(directory);
var paths = [];
tree.children.forEach(child => {
paths.push('./'+child.path);
})
return paths;
}
This issue occurred because the second file contains internal format issue (not validated JSON)

Express.js app works fine locally but doesn't work when deployed on Heroku

I'm currently trying to deploy a music visualizer that I built. It works in all scenarios except when I click on a song to play from the search bar. I keep on getting '503 (Service Unavailable)'. Does anyone know what's up with my code? I've spent the past 6 hours on SO trying to find the answer, but I've come up with nothing.
Here are my Heroku logs:
2018-08-10T01:47:00.757554+00:00 app[web.1]: events.js:183
2018-08-10T01:47:00.757570+00:00 app[web.1]: throw er; // Unhandled 'error' event
2018-08-10T01:47:00.757572+00:00 app[web.1]: ^
2018-08-10T01:47:00.757573+00:00 app[web.1]:
2018-08-10T01:47:00.757574+00:00 app[web.1]: TypeError: Invalid non-string/buffer chunk
2018-08-10T01:47:00.757576+00:00 app[web.1]: at validChunk (_stream_writable.js:254:10)
2018-08-10T01:47:00.757577+00:00 app[web.1]: at PassThrough.Writable.write (_stream_writable.js:288:21)
2018-08-10T01:47:00.757578+00:00 app[web.1]: at PassThrough.Writable.end (_stream_writable.js:563:10)
2018-08-10T01:47:00.757581+00:00 app[web.1]: at emitThree (events.js:141:20)
2018-08-10T01:47:00.757582+00:00 app[web.1]: at DestroyableTransform.emit (events.js:217:7)
2018-08-10T01:47:00.757583+00:00 app[web.1]: at emitThree (events.js:136:13)
2018-08-10T01:47:00.757585+00:00 app[web.1]: at FfmpegCommand.emit (events.js:217:7)
2018-08-10T01:47:00.757586+00:00 app[web.1]: at emitEnd (/app/node_modules/fluent-ffmpeg/lib/processor.js:424:16)
2018-08-10T01:47:00.757588+00:00 app[web.1]: at /app/node_modules/fluent-ffmpeg/lib/processor.js:433:16
2018-08-10T01:47:00.757589+00:00 app[web.1]: at /app/node_modules/async/dist/async.js:473:16
2018-08-10T01:47:00.765330+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-08-10T01:47:00.765674+00:00 app[web.1]: npm ERR! errno 1
2018-08-10T01:47:00.766812+00:00 app[web.1]: npm ERR! yuen#1.0.0 start: `node server.js`
2018-08-10T01:47:00.766955+00:00 app[web.1]: npm ERR! Exit status 1
2018-08-10T01:47:00.767136+00:00 app[web.1]: npm ERR!
2018-08-10T01:47:00.767256+00:00 app[web.1]: npm ERR! Failed at the yuen#1.0.0 start script.
2018-08-10T01:47:00.767393+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-08-10T01:47:00.771128+00:00 app[web.1]:
2018-08-10T01:47:00.771274+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-08-10T01:47:00.771356+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2018-08-10T01_47_00_768Z-debug.log
Here is the source code for my Express app (server.js):
var express = require('express');
var youtubeStream = require('youtube-audio-stream');
var app = express();
app.use(express.static(__dirname));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/stream/:videoId', function (req, res) {
try {
youtubeStream(req.params.videoId).pipe(res);
} catch (exception) {
res.status(500).send(exception);
}
});
app.listen(process.env.PORT || 3000, function () {
console.log('app is listening on port 3000!');
});
Here is the code that actually interacts with the server by sending xhr requests:
function loadSound(vidID) {
context.close()
.then(() => {
context = new AudioContext();
analyser = context.createAnalyser();
analyser.fftSize = 32;
})
.then(() => {
var request = new XMLHttpRequest();
request.open("GET", window.location.href+"stream/"+vidID, true);
request.responseType = "arraybuffer";
request.onload = function() {
var Data = request.response;
process(Data);
};
request.send();
});
}
function process(Data) {
source = context.createBufferSource();
context.decodeAudioData(Data, function(buffer){
source.buffer = buffer;
source.connect(context.destination);
source.connect(analyser);
source.start(context.currentTime);
});
document.getElementById('searchBar').disabled = false;
loading(true);
}
And lastly, here are the listed dependencies in my package.json file:
"dependencies": {
"express": "^4.16.3",
"ffmpeg": "0.0.4",
"fluent-ffmpeg": "^2.1.2",
"through2": "^2.0.3",
"xtend": "^4.0.1",
"youtube-audio-stream": "https://github.com/cryptagoras/youtube-audio-stream/archive/patch-3.tar.gz",
"ytdl-core": "^0.24.0"
}
I found the answer: I didn't actually have any of the encoding libraries installed on Heroku. I simply added ffmpeg into my .buildpacks, and now everything works like a charm!

Can not find module "sleep", with Leap motion

I'm trying to connect my Android project with Leap motion controller and am using the procedure and source mentioned on this question as reference.
I'm very new to Node.js and am unable to get the JavaScript file to work with Node and am unable to resolve dependencies. I tried node update but that didn't work. I need to resolve the following error:
$ node index.js
module.js:338
throw err;
^
Error: Cannot find module 'sleep'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent/index.js:11:13)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
Here's the JavaScript file for reference:
var http = require('http'), express = require('express'), app = express(), server = http
.createServer(app), port = process.env.PORT || 5000, position = {
x : 0,
y : 0,
z : 0,
hand : '',
present : false
};
var updated = false;
var sleep = require('sleep');
server.listen(port, function() {
console.log('Listening on ' + port);
});
function hasUpdate() {
if (updated) {
updated = false;
return true;
}
return false;
}
var BinaryServer = require('binaryjs').BinaryServer;
var binaryServer = new BinaryServer({
port : 9000
});
binaryServer.on('connection', function(client) {
console.log('accept binary connection');
var stream = client.createStream();
var buffer = [];
console.log('got stream');
stream.on('data', function(data) {
buffer.push(data);
var text = buffer.join();
var end = text.lastIndexOf('}');
if (end > -1) {
var temp = text.substr(0, end+1);
var start = temp.lastIndexOf('{');
if (start > -1) {
var message = text.substr(start, end+1);
var o=JSON.parse(message);
updateHandPosition(o);
if (end +1 < buffer.length) {
buffer = buffer.splice(end + 1, buffer.length);
}else{
buffer=[];
}
}
// console.log('received data from client ' + message + ' ' + end
// + ' ' + start + ' ');
}
});
});
app.use(express.bodyParser());
app.get('/', function(request, response) {
response.sendfile('public/index.html');
});
function updateHandPosition(o){
position.present = o.present;
if (position.present) {
position.x = o.x;
position.y = o.y;
position.z = o.z;
position.hand = o.hand;
} else {
position.x = null;
position.y = null;
position.z = null;
position.hand = null;
}
updated=true;
// console.log('updating hand position '+JSON.stringify(position));
}
app.post('/update', function(request, response) {
updateHandPosition(response.body);
response.json({
success : true
});
updated = true;
});
app.get('/retrieve', function(request, response) {
response.json(getPositionData());
});
function getPositionData() {
if (position.present) {
return {
present : true,
x : position.x,
y : position.y,
z : position.z,
hand : position.hand
};
} else {
return {
present : false
};
}
}
function forever(request, response) {
this.response = response;
this.request = request;
var ref = this;
this.request.on('close', function() {
ref.clear();
});
this.request.on('end', function() {
ref.clear();
});
};
forever.prototype.clear = function() {
this.request = null;
this.response = null;
};
forever.prototype.stream = function() {
if (this.response != null) {
if (hasUpdate()) {
this.response.write(JSON.stringify(getPositionData()));
this.response.write(",");
}
var ref = this;
setTimeout(function() {
ref.stream();
}, 5);
}
};
app.get('/stream', function(request, response) {
var f = new forever(request, response);
f.stream();
});
app.get(/^(.+)$/, function(req, res) {
res.sendfile('public/' + req.params[0]);
});
I tried using "node install sleep" but I get this error log:
npm WARN package.json LeapCallController#0.1.0 No repository field.
/
> sleep#2.0.0 install /home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent/node_modules/sleep
> node-gyp rebuild
gyp ERR! configure error
gyp ERR! stack Error: "pre" versions of node cannot be installed, use the --nodedir flag instead
gyp ERR! stack at install (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/install.js:66:16)
gyp ERR! stack at Object.self.commands.(anonymous function) [as install] (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/node-gyp.js:66:37)
gyp ERR! stack at getNodeDir (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:152:20)
gyp ERR! stack at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:95:9
gyp ERR! stack at ChildProcess.exithandler (child_process.js:723:7)
gyp ERR! stack at ChildProcess.emit (events.js:110:17)
gyp ERR! stack at maybeClose (child_process.js:1000:16)
gyp ERR! stack at Socket.<anonymous> (child_process.js:1168:11)
gyp ERR! stack at Socket.emit (events.js:107:17)
gyp ERR! stack at Pipe.close (net.js:461:12)
gyp ERR! System Linux 3.16.0-30-generic
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent/node_modules/sleep
gyp ERR! node -v v0.13.0-pre
gyp ERR! node-gyp -v v1.0.1
gyp ERR! not ok
npm ERR! sleep#2.0.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the sleep#2.0.0 install script.
npm ERR! This is most likely a problem with the sleep package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls sleep
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 3.16.0-30-generic
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "sleep"
npm ERR! cwd /home/shivin/Development/LeapJS code/LeapMotionAndroidBridge/WebContent
npm ERR! node -v v0.13.0-pre
npm ERR! npm -v 1.4.28
npm ERR! code ELIFECYCLE
npm ERR! not ok code 0
If you are not using sleep remove this line
var sleep = require('sleep');
or install the sleep module using
npm install sleep
and restart the node project

Rendering view with all the data using jade,express and monk

My mongodb database has two collections notes which holds each note and notebook which has a title and an array of notes containing an array of ids pointing to the note in the notes collection.
//notes:
{
"_id" : ObjectId("542afaaa4898f4042312872b"),
"title" : "Hello Universe",
"content" : "Saving to database"
}
//notebook
{
"_id" : ObjectId("53d0c6d8daeac43cf8412945"),
"title" : "My Notebook",
"notes" : [
ObjectId("53d0ca2fdaeac43cf8412946"),
ObjectId("542afe34a25e3c44160e6b05"),
ObjectId("542b7908a459766020342eae"),
ObjectId("542b79efa459766020342eaf"),
ObjectId("542b7a5ba459766020342eb0"),
ObjectId("542b7aa3d3b135c022b674ff"),
ObjectId("542bb47aa4ff29c820c22fdc"),
ObjectId("542bb720a4ff29c820c22fdd"),
ObjectId("542bddd5712c0dc426d41341"),
ObjectId("542bdec5712c0dc426d41342")
]
}
I use these to retrieve data to render this jade view.This is the code for my route:
router.get('/main',function(req,res){
var db=req.db;
var notebookCollection=db.get('notebook');
var notesCollection=db.get('notes');
var locals={};
locals.title="TakeNote:An easy to use note taking app for web and hopefully mobile";
locals.sidebar={};
locals.sidebar.items=[];
//get data for the first notebook and display it by default
locals.notebook={};
locals.notebook.notes=[];
notebookCollection.find({},{},function(err,docs){
if(err)
console.error(err);
else
{
console.log("Retrieving notebooks");
var noteIds;
if(docs.length)
{
console.log(docs)
for(var i=0;i<docs.length;i++)
{
locals.sidebar.items.push(docs[i].title);
locals.notebook.title=docs[i].title;
noteIds=docs[i].notes;
for(var j=0;j<noteIds.length;j++)
{
notesCollection.findOne({_id:noteIds[j]},function(err,doc){
if(err)
console.error(err);
else
{
locals.notebook.notes.push(doc);
//res.render('main',locals);
}
});
//putting it here might mean that it renders without any of the notes
//putting it here with a condition fails
/*
if(j===noteIds.length-1)
res.render('main',locals);
*/
}
}
//putting it here means it renders without any of the notes
}
}
});
});
Where can I call res.render('main',locals) so that it renders the template with all my data here?
EDIT Error when using async:
I get this error when using the async library:
E:\Do\hello_express\node_notes\node_modules\async\lib\async.js:641
tasks[index].apply(null, arguments);
^
TypeError: Object #<Promise> has no method 'apply'
at fn (E:\Do\hello_express\node_notes\node_modules\async\lib\async.js:641:34
)
at Object._onImmediate (E:\Do\hello_express\node_notes\node_modules\async\li
b\async.js:557:34)
at processImmediate [as _immediateCallback] (timers.js:336:15)
\
npm ERR! node_notes#0.0.1 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the node_notes#0.0.1 start script.
npm ERR! This is most likely a problem with the node_notes package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get their info via:
npm ERR! npm owner ls node_notes
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7600
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! cwd E:\Do\hello_express\node_notes
npm ERR! node -v v0.10.29
npm ERR! npm -v 1.4.14
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! E:\Do\hello_express\node_notes\npm-debug.log
npm ERR! not ok code 0
This is my async code:
function getActiveNotes(doc)
{
var noteIds=doc.notes;
async.map(noteIds,function(_id){
notesCollection.findOne({_id:_id})
},function(err,docs){
if(err)
console.error(err);
else
{
if(docs)
locals.notebook.notes=docs;
}
});
}
function getAllNotebooks()
{
notebookCollection.find({},{},function(err,docs){
if(err)
console.error(err);
else
{
if(docs)
{
for(var i=0;i<docs.length;i++)
{
locals.sidebar.items.push(docs[i].title);
}
}
}
});
}
function activeNotesOp(){
async.waterfall([notebookCollection.findOne({isActive:true}),getActiveNotes]);
}
async.parallel([getAllNotebooks,activeNotesOp],function(err,results){
if(err)
console.error(err);
else
console.log(results);
});
On reflection parallel may not be what you need, but I think this is a useful skeleton for you.
var notebooksIdx = 0;
notebookCollection.find({},{},function(err,notebooks){
async.each(
notebooks,
, function(notebook, cb) {
notesCollection.find({_id: {$in: notebook.notes}}, function(err, notes) {
if (err)
return cb(err);
return locals.notebook[notebooksIdx++].notes = notes;
})
}
, function(err) {
if (err) throw err;
res.render(....)
}
});
});
I used lodash method forEach to obtain the title of each notebook to populate the locals.sidebar,I used lodash.filter(this returns an array,obtained the first item) to find the activeNotebook and retrieve all the notes from it using the $in operator to perform a query for all the notes contained by the notebook and populated locals.notebook.notes with that data:
notebookCollection.find({},{},function(err,notebooks){
if(err)
console.error(err);
else
{
_(notebooks).forEach(function(notebook){
locals.sidebar.items.push(notebook.title);
});
//console.log(locals.sidebar.items);
var activeNotebooks= _.filter(notebooks,function(notebook){
return notebook.isActive;
});
var notes=activeNotebooks[0].notes;
notesCollection.find({_id:{$in:notes}},function(err,docs){
if(err)
console.error(err);
else
{
if(docs.length)
{
locals.notebook.notes=docs;
console.dir(locals);
res.render('main',locals);
}
}
});
}
});

Categories