Argument "data" is not a valid Document. Input is not a plain JavaScript object - node.js

I am getting below error .
Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.
at Object.exports.(anonymous function) [as isDocument] (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/validate.js:86:15)
at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/write-batch.js:286:14)
at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/reference.js:420:8)
at PainEntryService.createEntry (/user_code/services/entry-service.js:19:30)
at /user_code/services/intentService.js:22:36
at Function.<anonymous> (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:146:23)
at next (native)
at /user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:22:71
at __awaiter (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:18:12)
at Function.handler (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:84:16)
I am using below code for creating the document
var docRef = this.dbManager.collection('tetst).doc(subjectId);
var setAlan = docRef.set(EntryEntity.toJSON);
I have below code in the toJSON method
public toJSON(): testEntry {
let returnJSON = {
"entry_id": this.entry_id,
"subject_id": this.subject_id,
"entry_date": this.entry_date,
"questionnaire": this.questionnaire,
"entry_start_timestamp": this.entry_start_timestamp,
"entry_end_timestamp": this.entry_end_timestamp,
"entry_complete": this.entry_complete,
//"responses": this.responses,
"last_answered_question" : this.last_answered_question,
"entry_status" : this.entry_status
}
return returnJSON;
}
I am framing json object in the above method. If i print the json object, i am getting below content
{ entry_id: 'df2b4ad4-6a70-4304-a71f-3a63773ada61',
subject_id: 'ABwppHHkzfY1Whp-lCHNnvEcuqvsbMKtZsg_ui9vc4jtpXSiAbh0fNsg6LxGkYq-Va3SOrwcvD-HAs7VQA',
entry_date: '2018-06-15',
questionnaire: 1,
entry_start_timestamp: '2018-06-15T09:38:10.266Z',
entry_end_timestamp: '2018-06-15T09:38:10.266Z',
entry_complete: false,
last_answered_question: 0,
entry_status: 'active' }
How to solve above issue? is there any issue with json object?

You need to call the toJSON method. Currently you are passing a reference to a method to set when you need to pass the return value of the called method.
Change
var setAlan = docRef.set(EntryEntity.toJSON);
to
var setAlan = docRef.set(EntryEntity.toJSON());

Related

Using JSON.stringify on [object Object] gives "[object Object]" on React

I am trying to pass an object data from one page to another.
I have a line of code that can pass id and I tried to use it to pass object rather than an integer id but I can't get it right.
The code for passing id from source page:
const navigate = useNavigate();
id && navigate(generatePath("/employeelistedit/:id", { id })); //sample code which works fine when used
The code for passing the object data from source page copying the format of the code above:
function sourcePage(){
const navigate = useNavigate();
var values = {id: "someData", startd: "someData", endd: "someData"}
values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));
    } //with useNavigate and generatePath
This is the code in another page which receives the data:
const { values } = useParams(); //values gives [object Object]
const x = JSON.stringify(JSON.stringify(values)) //gives "[object Object]"
const y = Object.prototype.toString.call(values) //gives [object String]
For my routing, this is how I wrote it:
<Route path="/harvestcalendarmonitoring/:values" element={< Harvestcalendarmonitoring />} /> //refers to the receiving page
I know I'm not doing it right cause I know that "[object Object]" is showing that something is wrong somewhere in my codes.
Any help and suggestions would be really much appreciated. Thank you in advance.
It looks like you missed the stringify step:
function sourcePage(){
const navigate = useNavigate();
var values = JSON.stringify({id: "someData", startd: "someData", endd: "someData"});
values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));
} //with useNavigate and generatePath
However, make sure generatePath is also URL encoding this string values or else you will likely have an invalid URL.
When it comes time to parsing the string back into an object, be sure to call JSON.parse
With the help of Steve through his comment above/below this comment,
use JSON.stringify() for the object before passing and receive it using JSON.parse().
Code in source page:
values = JSON.stringify(values);
values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));
Code in receiving page:
const {values} = useParams();
const w = JSON.parse(values) //the w variable gives the desired and/or expected object data

How to print the object value which return type is [Object , Object] in Nod JS?

below is one piece of code where i have to compare one object stored value (i.e 'resort') with the value compare that value what i am getting from the JSON file .
code -
resort = _.find(this.resorts.entries, (o) => {
return o.gqe_name === resort;
})
;
i have tried to get the value but the it is displaying as [Object,Object ] , tried with console .log('resort'+ resort) and log.info ('resort'+ resort).
is there any way i can view the return value ?
how i can print json stored value 'o.gqe_name' ?
JSON.stringify can help
const object = { test: { test2: 'value' } }
const result = JSON.stringify(object)
const result2 = JSON.stringify(object, null, 2)
console.log(result)
console.log(result2)

Convert the object into a string and bring in the necessary form

An object comes to me. I write the variable key and the value through the loop:
let update_info = [];
for (let[key, value] of Object.entries(req.body)) {
update_info.push(`${key} = ${value}`);
}
console.log(JSON.parse(update_info));
Output to console:
undefined:1
user_name = name,user_email = #email.com,user_password = 12345678,about = aboutaboutaboutabout
^
SyntaxError: Unexpected token u in JSON at position 0
Why it happens?
I need to be displayed in the console like this:
'user_name' = 'name','user_email' = '#email.com','user_password' = '12345678','about' = 'aboutaboutaboutabout
How do i implement this?
I've reproduced your code like this and all you need to do is
JSON.stringify turns a JavaScript object into JSON text and stores that JSON text in a string.
JSON.parse turns a string of JSON text into a JavaScript object.
let obj = {
"welcome": "hello",
"reply": "hi"
}
let update_info = [];
for (let[key, value] of Object.entries(obj)) {
update_info.push(`${key} = ${value}`);
}
console.log(JSON.stringify(update_info));
Try the below code:
You need not to parse it using JSON.parse because the array is not yet stringified, so you should use toString() to achieve the desired result
let update_info = [];
for (let[key, value] of Object.entries(req.body)) {
update_info.push(`'${key}' = '${value}'`);
}
console.log(update_info.toString());
If you are interested in printing the Object key value pair in the console to have better view use
console.log(JSON.stringify(object, undefined, 2))
This will print the object in proper format indented by 2 spaces

Cannot read property call of undefined at Blockly.Generator.blockToCode

I am trying to add a custom text block. But when i enter any text in the input field, the error comes up.
"Uncaught TypeError: Cannot read property 'call' of undefined"
Blockly.Blocks['text_input']={
init:function()
{
this.appendDummyInput()
.appendField('Text Input:')
.appendField(new Blockly.FieldTextInput(''),'INPUT');
this.setColour(170);
this.setOutput(true);
}
};
this happens when the language definition for your custom type is missing.
// Replace "JavaScript" with the language you use.
Blockly.JavaScript['text_input'] = function(block) {
var value = Blockly.JSON.valueToCode(block, 'INPUT', Blockly.JavaScript.ORDER_NONE);
// do something useful here
var code = 'var x= "bananas"';
return code;
};

Is it possible to pass JSON object to ejs renderFile?

I'd rather not have to type out each field name of my ejs file. Here is what i'd like to do:
let html = null;
EJS.renderFile('./public/views/results.ejs', {JSON_OBJECT}, (err, str) => {
html = str;
});
Something encapsulated in {} will create a new object.
So in
var JSON_OBJECT = {test: "value"};
var obj2 = {JSON_OBJECT}
obj2 will be an object with a property named JSON_OBJECT, which in itself has a property test.
EJS.renderFile accepts a JSON object as its second parameter. The properties the object can contain are described in the package description of ejs.

Resources