In your first method, it is not $u \cos\theta$ the velocity in the vertical direction of mass $M$. If you consider the mass $M$ goes up with velocity $v$ , $v \cos\theta$ is the velocity component along the string. It is equal to $u$ as the string moves with that speed. So you get $$ v \cos\theta =u$$
$$v=\frac{u}{cos\theta}$$
To elaborate my answer:
Here you may be wondering why $v \cos \theta$ is incorrect and $\frac{v}{\cos\theta}$ is correct. Before moving on to your example you need to identify what the velocity is. Velocity is the rate at which the displacement changes. Also the velocity component is the effect on the direction other than the direction of the resultant velocity. There must be at least two components to represent the resultant velocity.
Now let's go to your example. Obviously $\theta$ is not constant here. This is explained by the fact that the velocity is zero when the strings are horizontal. If $\theta$ is somehow constant then you get the speed of the object as $2u\cos\theta$ according to the vector sum. Here the speed is constant. This is even clearer if the incident is considered in the opposite direction. The object travels at a speed of $2u\cos\theta$, which means $2u\cos\theta$ is the vertical ascent per second (according to your picture). It can also be interpreted as follows. The object goes along the direction which is inclined to vertical by angle $\theta$ to the left, distance $u$, and then moves to the direction which is inclined to the vertical by angle $\theta$ to the right, distance $u$ . The result is the same. I explained this to give a brief idea about velocity and velocity components.
Now let's consider your example again. You clearly know that the object here is moving vertically upwards. Therefore you should mark the velocity of the object vertically upwards. And you know something else important. Although this velocity is not constant, its $\cos\theta$ component is constant, because the strings travel in that direction and the velocity of the strings is constant. It is equal to $u$. So you can take $v\cos\theta=u$.
And now, why $v=u\cos\theta$ is incorrect?
It's because you can't think the object is moving in that direction along the string. This cause severe misunderstanding. If you think $v=u\cos\theta$, then it means the object is moving along a string. But which string? That is why it's wrong.
I think this is clear now.