Giving the following snippets of object below:
var s;
var tab = [];
var myarray= [];
for(var i=0;i<=tab.length-1;i++){
s= "{\"id\":\"" + tab[i][0] + "\",\"ts\":\"" + tab[i][1] + "\",\"lat\":\"" + tab[i][2] + "\",\"lon\":\""+tab[i][3]+"\"}";
myarray.push(s);
}
myarray = [ '{"id":"id1","ts":"ts1","lat":"lat1","lon":"lon1"}',
'{"id":"id2","ts":"ts2","lat":"lat2","lon":"lon2"}',
'{"id":"id3","ts":"ts3","lat":"lat3","lon":"lon3"}' ]
I want to save it in mongo database by using mongoose, this is my code:
for(var obj in myarray){
var doc = new MyCollection(myarray[obj])
doc.save()
.catch((err=>{console.log(error.message);}))
}
But I get this error when executing the code:
TypeError: Cannot use 'in' operator to search for '_id' in {"id":"id1","ts":"ts1","lat":"lat1","lon":"lon1"}
I don't find the source of the error. Can you help me please?
'around it. Seems like you have a parser problem somewhere. Or something doing a.toString()where it should not be.Objectthen you would just be doingfor (var obj in myarray) { var doc = new MyCollection(obj); .....since its the actualObjectand not the "keyname" or "index" that you are getting from theforJSON.parsemay be a quick fix, but it's going to confuse anyone reading the code, wondering why the line is put in there. So you "should" track down the source of how these came to be in strings in the first place.