0

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?

4
  • 4
    That's not an object, that's a "string". See the quotes ' around it. Seems like you have a parser problem somewhere. Or something doing a .toString() where it should not be. Commented May 29, 2017 at 9:12
  • And if it were an Object then you would just be doing for (var obj in myarray) { var doc = new MyCollection(obj); ..... since its the actual Object and not the "keyname" or "index" that you are getting from the for Commented May 29, 2017 at 9:15
  • Thx! I just parsed to JSON each object of my array then i got single quotes disappeared Commented May 29, 2017 at 10:23
  • That is expected. The real thing you should be looking at is how they got there in the first place. Inserting a JSON.parse may 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. Commented May 29, 2017 at 10:26

3 Answers 3

1

in myarray you have enclosed object using single quotes.remove it and try. your myarray should be like this.

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"} ];

for(var obj in myarray){
       var doc = new MyCollection(myarray[obj])
        sigfoxData.save()
                  .catch((err=>{console.log(error.message);}))
            }
Sign up to request clarification or add additional context in comments.

1 Comment

Thx! I just parsed to JSON each object of my array then i got single quotes disappeared
0

Hope this helps

 var 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"} ];

    Model.create(myarray, function (err) {
        if (err) {
            response.send({error: true, message: err});
        } else {
            response.send({success: true});
        }
    });

Comments

0

I got solution by doing this:

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(JSON.parse(s));
            }

So i just parsed the s string!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.