I am new in node js. I am getting a typeerror while using .get function. The error is
TypeError: item.get is not a function
Code --
query.lean().exec(function (err, data)
{
JSON.stringify(data);
callback(null,data,count);
});
function(data,count,callback)
{
//...some code
callback(null,[count,data]);
}
function(docs, callback){
console.log(docs[1]);
async.each(docs[1],function(item,cb) {
if (typeof(item.video.path)!="undefined") {
item.players.cover = config.general+ item.video.path;
}
});
}
I did console.log(docs[1]) and the video.path exist within the json object.Before using lean() this set and get was working but not now.
Any help is highly appreciated. Thanks in advance.
If item is an object de-serialised from JSON, then there's no way it has any methods or functions.
I suspect you just want to set a property on the object. Something like
if (typeof item.video.path !== 'undefined') {
// ensure item.players is an object first
item.players = item.players || {}
// now set the value
item.players.cover = config.general + item.video.path
}
Related
I am storing particular key value in Database. But, While fetching the key value, getting undefined error.
await DbHandler.fetch(codeStatus)
.then((result) => {
const codeEnabledObj = result[0];
console.log('codeEnabledObj', codeEnabledObj);
let codeEnabled = false;
if (codeEnabledObj && codeEnabledObj.length > 0) { // this code not executing at all.
codeEnabled = codeEnabledObj[0].isEnabled;
}
console.log('codeEnabled', codeEnabled); // getting false always
console.log('codeEnabledObj.length[0]', codeEnabledObj.length); // undefined
})
.catch((error) => {
});
The issue is, It's not going inside if condition and throwing error like undefined.
But, If we print the response from db fetch
'codeEnabledObj', { type: 'codeStatus', isEnabled: true } // This is my response
Any suggestions?
Objects don't have length property like an array.
codeEnabledObj.length is wrong
use this,
Object.keys(codeEnabledObj).length
EDIT :
codeEnabledObj[0].isEnabled should be only codeEnabledObj.isEnabled
There is no property length in the codeEnabledObj , Moreover its not an Array.. so modifying the condition would work, where isEmpty could be a function used from
node package as underscore
if (isEmpty(codeEnabledObj)) { // ... }
and
codeEnabledObj.isEnabled
Thanks.
I am writing a function that is meant to add an employee to the end of a list of employees, but I continue to be met with the error in the title. I've tried to alter the code, but I'm not sure what I'm doing wrong. Here's the function:
data-service.js
module.exports.addEmployee = function(employeeData) {
employeeData.employeeNum = ++empCount;
return new Promise(function(resolve,reject) {
employees.push(employeeData);
if(employees.length == 0) {
reject("no results returned");
}
resolve(employeeData);
});
}
server.js
app.get("/employees/add", (req,res) => {
res.render("addEmployee");
});
app.post("/employees/add", (req, res) => {
console.log(req.body);
res.redirect("/employees");
});
The current function is not the root of the problem... However, you are trying to set a property on a param that you expect to be an object. But the caller has either passed a variable that has a value === undefined, or perhaps is passing no params at all ( either way, the param employeeData is undefined and you have no checks against it, thus we see the error).
I am trying to do maths on a number within a JSON object (the price of a stock ticker).
I want it to be a variable called 'btcusd_price', that I can then use to do arithmetic with.
How do I get a variable i can work with?
https://tonicdev.com/npm/bitfinex
var Bitfinex = require('bitfinex');
var bitfinex = new Bitfinex('your_key', 'your_secret');
var btcusd_price;
btcusd_price = bitfinex.ticker("btcusd", function(err, data) {
if(err) {
console.log('Error');
return;
}
console.log(data.last_price);
return data.last_price;
});
typeof btcusd_price;
console.log(btcusd_price); //i'm trying to work with the price, but it seems to be 'undefined'?
You have to set the values when they are available, the code is async and you were using the value before it is applied to btcusd_price. Note that the proper way to use the variable is when the callback executes its code.
Please, see the working code below:
Bitfinex = require('bitfinex');
var bitfinex = new Bitfinex('your_key', 'your_secret');
var btcusd_price;
bitfinex.ticker("btcusd", function(err, data) {
if(err) {
console.log('Error');
return;
}
btcusd_price = data.last_price;
console.log(typeof btcusd_price);
console.log(btcusd_price);
});
I am trying to update one particular field in the document. Here is my code:
module.exports.editAndUpdate = function(playerVideo, callback) {
console.log(playerVideo.vidName);
var newVidId = toString(playerVideo.vidId);
myModel.findById(newVidId, function(err, doc) {
if(err) throw err;
doc.vidName = playerVideo.vidName;
doc.save(callback);
});
}
Here playerVideo is a document, vidName and vidId are its fields.
Instead of doc.vidName = playerVideo.vidName, if I use doc.vidName = "Hey", then it is working perfectly fine. I used the typeof method and checked if playerVideo.vidName is a String and yes, it is.
Please help me solve this. Thanks for any suggestions :)
For me, the issue occurred when I was updating a nested object, which had a fullstop in the key value.
e.g.
"$set": {
"some.field": {
"validuser": "this works",
"invalid.user": "this breaks mongo"
}
}
MongoDB doesn't support keys with a dot, and was providing this cryptic error:
cannot set property 'value' of null
Solution - don't store keys with a fullstop in it!
I am trying to use this getMapping function seen here in the api. I am trying to get the mapping for an index in my database. So far I've tried this
var indexMap = client.indices.getMapping(['indexName'], function() {
console.log(indexMap);
});
and
var indexMap = client.indices.getMapping({index: 'indexName'}, function() {
console.log(indexMap);
});
both tries fail and log { abort: [Function: abortRequest] }
So I took a closer look at the ElasticSearch JS Quick Start docs to see how they were using the methods. I was confused by the API because I thought it was supposed to take an array client.indices.getMapping([params, [callback]]). But I now understand that it takes an object with params inside that object and then returns the response in a callback. The function does not return anything relevant as far as I can tell. Heres the code I used to get the mapping on 'myIndex' index. The mapping is stored in the response object.
Code:
client.indices.getMapping({index: 'patents'}, function(error, response) {
if (error) {
console.log(error);
} else {
console.log(response);
}
});