Node: all data is saved except the array field - node.js

I have a problem saving this object that has an array as a property, everything is being saved except the array.I don't know what point I'm missing, if the problem was the approach to solving the problem if there is a better way to solve it
json sent
data:'2020-08-14'
hora:'21:04'
identificador:'MSG 001'
mensagem:'TEST 001'
periocidade:(7) ['Segunda-Feira', 'Terça-Feira', 'Quarta-Feira', 'Quinta-Feira', 'Sexta-Feira', 'Sabado', 'Domingo']
__proto__:Object```
Model
``new Schema({
identificador: {
type: String
},
hora: {
type: String
},
data: {
type: String
},
mensagem: {
type: String
},
ativo: {
type: Boolean,
default :true
},
periodicidade : [{
type : String
}],
date_time : {
type : Date,
default: Date.now
}``
Action
``routes.route('/add').post(function(req, res) {
let regra = new Mensagem(req.body);
regra.save()
.then(regra => {
res.status(200).json({'msg': 'added successfully'});
})
.catch(err => {
res.status(400).send('adding new failed');
});
});``

Related

Expected 'property' to be of type string, instead found type object - Dynamoose

I am working with AWS DynamoDB and Dynamoose trying to fetch records using Scan function, but facing an issue that is not recognizable for me.
Stragenly, it's able to fetch records from another table in the same way and successfully get the records.
Here's my Code:
const vehicleMasterSchema = new dynamoose.Schema({
"id": String,
"customer_account_number": String,
"fuel_type": String,
"make": String,
"model": String,
"odometer_gatex": String,
"plate_no": String,
"rfid_gatex": String,
"sales_agreement_id": String,
"vehicle_category": String,
"vehicle_id": String,
}, {
"timestamps": {
"createdAt": "create_date",
"updatedAt": null // updatedAt will not be stored as part of the timestamp
}
});
const vehicleMasterModel = dynamoose.model("vehicle_master", vehicleMasterSchema, { "create": false });
router.post('/getFuelingStatus', (req, res) => {
var companyInfo = req.body;
try {
console.log(typeof vehicleMasterModel);
vehicleMasterModel.scan("customer_account_number").eq(companyInfo.customerId).exec((error, results) => {
if (error) {
console.error(error);
} else {
res.json(results);
}
});
} catch (error) {
res.json(error);
}
});
The TypeMismatch error is coming up only for this model same code is working for the other table.
Console Error
My Table
This appears to be related to this github issue on Dyanmoose
My guess is that the problem could be related with the name of your attribute, model.
In fact, this is the actual case: the following code, extracted from the source code in Document.ts is the one which is overwriting your model property:
Object.defineProperty(this, "model", {
"configurable": false,
"value": model
});
This is how the Document looks like before:
And after the execution of the aforementioned code:
This code is executed when processing the Scan exec function in DocumentRetriever.ts when the library maps every Item returned by DynamoDB to their internal Document representation, exactly in this line of code:
const array: any = (await Promise.all(result.Items.map(async (item) => await new this.internalSettings.model.Document(item, {"type": "fromDynamo"}).conformToSchema({"customTypesDynamo": true, "checkExpiredItem": true, "saveUnknown": true, "modifiers": ["get"], "type": "fromDynamo"})))).filter((a) => Boolean(a));
The error you reported is a consequence of that change when the type of the returned Item is checked against your schema model in the checkTypeFunction:
const {isValidType, matchedTypeDetails, typeDetailsArray} = utils.dynamoose.getValueTypeCheckResult(schema, value, genericKey, settings, {"standardKey": true, typeIndexOptionMap});
if (!isValidType) {
throw new Error.TypeMismatch(`Expected ${key} to be of type ${typeDetailsArray.map((detail) => detail.dynamicName ? detail.dynamicName() : detail.name.toLowerCase()).join(", ")}, instead found type ${typeof value}.`);
...
Please, try a different name, I think it will work properly.
Schema must be like this :
const ImageGalleryFoldersSchema = new Schema({
key: {
type: String,
hashKey: true,
required: true,
},
displayName: {
type: String,
required: true,
},
parentFolderKey: {
type: String,
required: false,
},
isActive: {
type: Boolean,
default: true,
required: false,
},
}, {
timestamps: true,
});
Maybe your problem is caused due to asynchronous behaviour.
To be more specific, I think that by the time you call the "scan"-function-chain the body-request has not been finished. However, due to the nature of Hoisting, the object "companyInfo" was already being initialised before you enter the function-call.
Therefore, you may get the specified "TypeMismatch"-error.
Could you please try implementing the following async/await-structure and tell me if this helps:
router.post('/getFuelingStatus', async (req, res) => {
var companyInfo = await req.body;
try {
console.log(typeof vehicleMasterModel);
vehicleMasterModel.scan("customer_account_number").eq(companyInfo.customerId).exec((error, results) => {
if (error) {
console.error(error);
} else {
res.json(results);
}
});
} catch (error) {
res.json(error);
}
});

Nested MongoDB document issue (Mongoose and Node Js)

I am facing some issues while inserting data into nested documents structure of mongoDb.
Following is the Mongoose Model:
const funnel = new mongoose.Schema({
funnelName:{
type:String,
unique:true
},
group: String,
category: String,
funnelStep: {
stepType: String,
stepName: String,
stepPath: String,
isTracking: Boolean,
viewsStorage: []
} })
Below is the push I am sending to Db:
router.post('/createFunnel',async (req,res)=>{
if(!req.body.funnelName || !req.body.group || !req.body.category)
{return res.status(422).json({error:"Please add all the fields."})}
try{
const funnelSteps = []
funnelSteps.push({
stepType: req.body.stepType,
stepName: req.body.stepName,
stepPath: req.body.stepPath,
isTracking: req.body.isTracking,
viewsStorage: req.body.viewsStorage
})
const funnels = new Funnel({
funnelName : req.body.funnelName,
group : req.body.group,
category : req.body.category,
funnelStep : funnelSteps
})
await funnels.save(function(err){
if(err){
return res.status(422).send({error: err.message})
}
return res.json(funnels)
})
} catch(err){
return res.status(422).send({error: err.message})
} })
Below is the data structure I am sending through postman:
{
"funnelName":"Name-Funnel",
"group":"AVC",
"category":"XYZ",
"funnelStep":[
{
"stepType":"Advert",
"stepName":"Angle",
"stepPath":"google.com",
"isTracking":1,
"viewsStorage":[0,0]
},
{
"stepType":"Optin",
"stepName":"Ver 1",
"stepPath":"fb.com",
"isTracking":1,
"viewsStorage":[1,0]
},
{
"stepType":"Check",
"stepName":"rev-cat",
"stepPath":"google.com",
"isTracking":0,
"viewsStorage":[2,0]
}
] }
Below is the output I am getting in response:
{
"funnelStep": {
"viewsStorage": []
},
"_id": "5ec0ff78a6dfab18f4210e96",
"funnelName": "Testing The Latest Method4",
"group": "AVC",
"category": "XYZ",
"__v": 0
}
How can I fix this issue as my data is not getting inserted properly?
And apart from this, in the viewsStorage array, how to store date and a number which will increment after a certain operations and will get saved in the array according to the dates?
I think there is an issue in the funnelSteps array creation part. You are trying to get data directly from req.body instead of req.body.funnelStep
const funnelSteps = []
req.body.funnelStep.forEach(fs => {
funnelSteps.push({
stepType: fs.stepType,
stepName: fs.stepName,
stepPath: fs.stepPath,
isTracking: fs.isTracking,
viewsStorage: fs.viewsStorage
})
})
Schema
const funnel = new mongoose.Schema({
funnelName:{
type:String,
unique:true
},
group: String,
category: String,
funnelStep: [{
stepType: String,
stepName: String,
stepPath: String,
isTracking: Boolean,
viewsStorage: []
}] })

How to grab field value during a MongooseModel.bulkWrite operation?

Context:
I am trying to upsert in bulk an array of data, with an additional computed field: 'status'.
Status should be either :
- 'New' for newly inserted docs;
- 'Removed' for docs present in DB, but inexistent in incoming dataset;
- a percentage explaining the evolution for the field price, comparing the value in DB to the one in incoming dataset.
Implementations:
data.model.ts
import { Document, model, Model, models, Schema } from 'mongoose';
import { IPertinentData } from './site.model';
const dataSchema: Schema = new Schema({
sourceId: { type: String, required: true },
name: { type: String, required: true },
price: { type: Number, required: true },
reference: { type: String, required: true },
lastModified: { type: Date, required: true },
status: { type: Schema.Types.Mixed, required: true }
});
export interface IData extends IPertinentData, Document {}
export const Data: Model<IData> = models.Data || model<IData>('Data', dataSchema);
data.service.ts
import { Data, IPertinentData } from '../models';
export class DataService {
static async test() {
// await Data.deleteMany({});
const data = [
{
sourceId: 'Y',
reference: `y0`,
name: 'y0',
price: 30
},
{
sourceId: 'Y',
reference: 'y1',
name: 'y1',
price: 30
}
];
return Data.bulkWrite(
data.map(function(d) {
let status = '';
// #ts-ignore
console.log('price', this);
// #ts-ignore
if (!this.price) status = 'New';
// #ts-ignore
else if (this.price !== d.price) {
// #ts-ignore
status = (d.price - this.price) / this.price;
}
return {
updateOne: {
filter: { sourceId: d.sourceId, reference: d.reference },
update: {
$set: {
// Set percentage value when current price is greater/lower than new price
// Set status to nothing when new and current prices match
status,
name: d.name,
price: d.price
},
$currentDate: {
lastModified: true
}
},
upsert: true
}
};
}
)
);
}
}
... then in my backend controller, i just call it with some route :
try {
const results = await DataService.test();
return new HttpResponseOK(results);
} catch (error) {
return new HttpResponseInternalServerError(error);
}
Problem:
I've tried lot of implementation syntaxes, but all failed either because of type casting, and unsupported syntax like the $ symbol, and restrictions due to the aggregation...
I feel like the above solution might be closest to a working scenario but i'm missing a way to grab the value of the price field BEFORE the actual computation of status and the replacement with updated value.
Here the value of this is undefined while it is supposed to point to current document.
Questions:
Am i using correct Mongoose way for a bulk update ?
if yes, how to get the field value ?
Environment:
NodeJS 13.x
Mongoose 5.8.1
MongoDB 4.2.1
EUREKA !
Finally found a working syntax, pfeeeew...
...
return Data.bulkWrite(
data.map(d => ({
updateOne: {
filter: { sourceId: d.sourceId, reference: d.reference },
update: [
{
$set: {
lastModified: Date.now(),
name: d.name,
status: {
$switch: {
branches: [
// Set status to 'New' for newly inserted docs
{
case: { $eq: [{ $type: '$price' }, 'missing'] },
then: 'New'
},
// Set percentage value when current price is greater/lower than new price
{
case: { $ne: ['$price', d.price] },
then: {
$divide: [{ $subtract: [d.price, '$price'] }, '$price']
}
}
],
// Set status to nothing when new and current prices match
default: ''
}
}
}
},
{
$set: { price: d.price }
}
],
upsert: true
}
}))
);
...
Explanations:
Several problems were blocking me :
the '$field_value_to_check' instead of this.field with undefined 'this' ...
the syntax with $ symbol seems to work only within an aggregation update, using update: [] even if there is only one single $set inside ...
the first condition used for the inserted docs in the upsert process needs to check for the existence of the field price. Only the syntax with BSON $type worked...
Hope it helps other devs in same scenario.

update array of object of object MongoDB

I have this model
student: {
package:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Package',
},
history: [
{
package: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Package',
},
orderDate: {
type: Date,
default: new Date().toLocaleDateString('id'),
},
Status: {
type: String,
default: 'Pending',
},
},
],
}
What I want to do is that I want to update Student.package and Student.history in 1 pass
I create this method in my model
StudentSchema.methods.updatePackage= function(idPackage) {
this.package = idPackage;
return this.save();
};
StudentSchema.methods.updateHistory= function(idPackage) {
this.history.push(idPackage);
return this.save();
};
and I'm trying to do something like this in my controller
buyPack: async (req, res, next) => {
try {
let dataStudent = await Student.findById('5b83443040e3751bb4e32a21');
await dataStudent.updatePackage(req.body);
await dataStudent.updateHistory(req.body);
return res.json(dataStudent);
} catch (err) {
console.log(err);
next(err);
}
},
I think the first and second methods are wrong, but I have tried to figure it out in almost half of day, but still no luck. What is the best way to achieve my goals?
Do I make my model wrong? or do the methods I created are wrong?
Mongoose model rename with plural .. and you refer with singular with "S", "Package" change it to "Packages"

Mongodb: Cannot see data of the Embedded Document via command

For mongodb's embedded document, I don't know why the data is not saved in the database or something else might be wrong? I tried to print out everything to make sure it works till the last step. But still got nothing when querying the embedded document, as you can see from below.
My schema:
// create competitorAnalysisSchema
var CompetitorAnalysis = new Schema({
firstObservation: { type: String },
secondObservation: { type: String },
thirdObservation: { type: String },
brandName: { type: String },
productCategory: { type: String },
photo1: { data: Buffer, contentType: String },
photo2: { data: Buffer, contentType: String },
photo3: { data: Buffer, contentType: String },
photo4: { data: Buffer, contentType: String }
});
// create UserSchema
var UserSchema = new Schema({
userName: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
currentDemo: { type: String },
nextDemo: { type: String },
startTime: { type: String },
startLocation: { type: String },
arriveTime: { type: String },
arriveLocation: { type: String },
leaveTime: { type: String },
leaveLocation: { type: String },
competitorAnalysis: [CompetitorAnalysis],
created_at: Date,
updated_at: Date
});
var User = mongoose.model('User', UserSchema);
module.exports = User;
In my index.js, all debug messages can be successfully printed out.:
// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')
// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
.put(function(req, res) {
// use our user model to find the user we want
User.findOne({ userName: req.params.userName}, function(err, user) {
if (err)
res.send(err);
console.log('Got the user!');
// update the text data
user.competitorAnalysis.firstObservation = req.body.firstObservation;
user.competitorAnalysis.secondObservation = req.body.secondObservation;
user.competitorAnalysis.thirdObservation = req.body.thirdObservation;
user.competitorAnalysis.brandName = req.body.brandName;
user.competitorAnalysis.productCategory = req.body.productCategory;
console.log('req.body.firstObservation: %s', req.body.firstObservation);
console.log('user.competitorAnalysis.firstObservation: %s', user.competitorAnalysis.firstObservation);
console.log('Save the text data for competitorAnalysisTextData!');
// save the user
user.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'User updated!' });
console.log('user.competitorAnalysis.firstObservation: %s', user.competitorAnalysis.firstObservation);
console.log('Finally save the User!');
});
});
})
As in console:
Got the user in "Put"!
req.body.firstObservation: 3
user.competitorAnalysis.firstObservation: 3
Save the text data for competitorAnalysisTextData!
user.competitorAnalysis.firstObservation: 3
Finally save the User!
Problem
However, when I search in my mongodb database, there is no data saved for the embedded document:
...
"leaveTime" : "Your Current Time:\n 2016-08-23 10:27:45 AM",
"leaveLocation" : "Your Current Address:\n 1\nInfinite Loop\nCupertino\n95014",
"competitorAnalysis" : [ ]
}
> db.users.find({"competitorAnalysis.firstObservation" : "3"}).pretty()
>
Empty here!
I'm new to mongodb. It'll be great if I can get some hints on where else I can check or what the problem might be.
Update
Output of collection:
> db.users.find().pretty()
{
"_id" : ObjectId("57ba5f41ad8858305a5d3e58"),
"created_at" : ISODate("2016-08-22T02:11:13.968Z"),
"updated_at" : ISODate("2016-08-24T19:42:56.311Z"),
"nextDemo" : "12:00pm - 3:00pm, Whole Foods Market, 5880 Centre Ave, Pittsburgh PA 15206",
"currentDemo" : "9:00am - 1:00pm, Whole Foods Market, 5880 Centre Ave, Pittsburgh PA 15206",
"password" : "<3da4dafc c96e05cd 855da8b3 ff0bf074 8156ec4b b9f1a002 ba907bcc d5e4aa5b fcd2fef9 dec240cd 86489978 7d85cec8 f11eae1c 7b60b2cc 6693da1a 4eae3a73>",
"email" : "chenya#gmail.com",
"userName" : "Chenya",
"__v" : 1,
"startLocation" : "Your Current Address:\n 10141\nBilich Pl\nCupertino\n95014",
"startTime" : "Your Current Time:\n 2016-08-24 03:42:42 PM",
"arriveTime" : "Your Arriving Time:\n 2016-08-24 03:42:44 PM",
"arriveLocation" : "Your Arriving Address:\n 10131\nBilich Pl\nCupertino\n95014",
"leaveTime" : "Your Current Time:\n 2016-08-23 10:27:45 AM",
"leaveLocation" : "Your Current Address:\n 1\nInfinite Loop\nCupertino\n95014",
"competitorAnalysis" : [ ]
}
>
These statements are the problem:
user.competitorAnalysis.firstObservation = req.body.firstObservation;
user.competitorAnalysis.secondObservation = req.body.secondObservation;
user.competitorAnalysis.thirdObservation = req.body.thirdObservation;
user.competitorAnalysis.brandName = req.body.brandName;
user.competitorAnalysis.productCategory = req.body.productCategory;
You're treating your competitorAnalysis array as if it were an object.
I don't work with Mongoose, so don't know the syntax, but you want to do something like this instead:
user.competitorAnalysis.push({
firstObservation: req.body.firstObservation,
secondObservation: req.body.secondObservation,
thirdObservation: req.body.thirdObservation,
brandName: req.body.brandName
productCategory: req.body.productCategory
});

Resources