2

I was trying to use the Node.js supertest to test some REST API.

request(app)
      .post("/products")
      .set(
        "Authorization",
        "Bearer my jwt token here"
      )
      .set("Content-Type", "multipart/form-data")
      .field("name", "Tomato")
      .field("userId", "5d921d306e96d70a28989127")
      .attach(
        "productImage",
        "D:/NodeJS/node-rest-shop/uploads/1558612339690managing-redis.jpg"
      )
      .expect(201)
      .then(res => {
        const body = res.body;
        expect(body).to.contain.property("message");
        expect(body).to.contain.property("productId");
        expect(body).to.contain.property("date");
        expect(body).to.contain.property("user");
        expect(body).to.contain.property("request");
        done();
      })
      .catch(err => done(err));

.field("userId", userId)

Is there any way to set the value of userId as a variable without setting a hardcoded string value? It is a MongoDB object id.

When I'm using value as a variable this error occurred.

TypeError: source.on is not a function
    at Function.DelayedStream.create (node_modules\delayed-stream\lib\delayed_stream.js:33:10)
    at FormData.CombinedStream.append (node_modules\combined-stream\lib\combined_stream.js:45:37)
    at FormData.append (node_modules\form-data\lib\form_data.js:74:3)
    at Test.RequestBase.field (node_modules\superagent\lib\request-base.js:406:23)
    at Context.done (test\api\product\product.js:77:8)

2 Answers 2

1

I can interpret this as 2 different ways, so I'll just answer both:

Can the value be specified as a non-string, e.g. a number.

multipart/form-data doesn't really have any sort of typing. Everything will be interpreted as a string usually. Your controller will need to do any conversion.

Can I use a variable in place of a hardcoded string.

Yes, you can.

Instead of:

.field("userId", "5d921d306e96d70a28989127")

You can just use:

.field("userId", userId)

As long as you defined a userId variable earlier

Sign up to request clarification or add additional context in comments.

6 Comments

When I'm using, .field("userId", userId) this error occured TypeError: source.on is not a function at Function.DelayedStream.create (node_modules\delayed-stream\lib\delayed_stream.js:33:10) at FormData.CombinedStream.append (node_modules\combined-stream\lib\combined_stream.js:45:37) at FormData.append (node_modules\form-data\lib\form_data.js:74:3) at Test.RequestBase.field (node_modules\superagent\lib\request-base.js:406:23) at Context.done (test\api\product\product.js:77:8)
What does userId contain?
usrId: new mongoose.Types.ObjectId()
new mongoose.Types.ObjectId() seems like it's an object. Maybe the object has a toString() method?
this is a node.js project. As the docs.mongodb.com/manual/reference/method/ObjectId states: An ObjectId must be a single string of 12 bytes or a string of 24 hex characters.
|
0

userId is a new mongoose.Types.ObjectId(). So, it does not return a string, it returns an object. You need to convert it to a string. We can use this. .field("userId", String(userId))

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.